-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstructStringFromBinaryTree.java
More file actions
43 lines (31 loc) · 1.3 KB
/
Copy pathConstructStringFromBinaryTree.java
File metadata and controls
43 lines (31 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// You need to construct a string consists of parenthesis and integers
// from a binary tree with the preorder traversing way.
// See: https://leetcode.com/problems/construct-string-from-binary-tree/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import leetcode.util.tree.TreeNode;
public class ConstructStringFromBinaryTree {
public String tree2str(TreeNode root) {
if (root == null) return "";
String left = "";
String right = "";
if (root.left != null && root.right != null) {
left = "(" + tree2str(root.left) + ")";
right = "(" + tree2str(root.right) + ")";
}
else if (root.left != null && root.right == null) {
left = "(" + tree2str(root.left) + ")";
} else if (root.left == null && root.right != null) {
left = "()";
right = "(" + tree2str(root.right) + ")";
}
return root.val + left + right;
}
public static void main(String[] args) {
ConstructStringFromBinaryTree sln = new ConstructStringFromBinaryTree();
TreeNode t1 = initTree(1, 2, 3, 4);
TreeNode t2 = initTree(1, 2, 3, null, 4);
System.out.println(sln.tree2str(t1));
System.out.println(sln.tree2str(t2));
}
}