|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.TreeNode;
|
4 | 4 |
|
| 5 | +import java.util.ArrayDeque; |
| 6 | +import java.util.Deque; |
| 7 | + |
5 | 8 | /**
|
| 9 | + * 536. Construct Binary Tree from String |
| 10 | + * |
6 | 11 | * You need to construct a binary tree from a string consisting of parenthesis and integers.
|
7 | 12 | The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis.
|
8 | 13 | The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
|
|
22 | 27 | There will only be '(', ')', '-' and '0' ~ '9' in the input string.
|
23 | 28 | An empty tree is represented by "" instead of "()".
|
24 | 29 | */
|
| 30 | + |
25 | 31 | public class _536 {
|
26 | 32 |
|
27 |
| - public TreeNode str2tree(String s) { |
28 |
| - if (s.equals("")) return null; |
29 |
| - int firstParen = s.indexOf("("); |
30 |
| - int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen)); |
31 |
| - TreeNode cur = new TreeNode(val); |
32 |
| - if (firstParen == -1) return cur; |
33 |
| - int start = firstParen; |
34 |
| - int leftParenCount = 0; |
35 |
| - for (int i = start; i < s.length(); i++) { |
36 |
| - if (s.charAt(i) == '(') leftParenCount++; |
37 |
| - else if (s.charAt(i) == ')') leftParenCount--; |
38 |
| - if (leftParenCount == 0 && start == firstParen) { |
39 |
| - cur.left = str2tree(s.substring(start + 1, i)); |
40 |
| - start = i + 1; |
41 |
| - } else if (leftParenCount == 0) { |
42 |
| - cur.right = str2tree(s.substring(start + 1, i)); |
| 33 | + public static class Solution1 { |
| 34 | + public TreeNode str2tree(String s) { |
| 35 | + if (s.equals("")) return null; |
| 36 | + int firstParen = s.indexOf("("); |
| 37 | + int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen)); |
| 38 | + TreeNode cur = new TreeNode(val); |
| 39 | + if (firstParen == -1) return cur; |
| 40 | + int start = firstParen; |
| 41 | + int leftParenCount = 0; |
| 42 | + for (int i = start; i < s.length(); i++) { |
| 43 | + if (s.charAt(i) == '(') { |
| 44 | + leftParenCount++; |
| 45 | + } else if (s.charAt(i) == ')') { |
| 46 | + leftParenCount--; |
| 47 | + } |
| 48 | + if (leftParenCount == 0 && start == firstParen) { |
| 49 | + cur.left = str2tree(s.substring(start + 1, i)); |
| 50 | + start = i + 1; |
| 51 | + } else if (leftParenCount == 0) { |
| 52 | + cur.right = str2tree(s.substring(start + 1, i)); |
| 53 | + } |
| 54 | + } |
| 55 | + return cur; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static class Solution2 { |
| 60 | + public TreeNode str2tree(String s) { |
| 61 | + Deque<TreeNode> stack = new ArrayDeque<>(); |
| 62 | + for (int i = 0, j = i; i < s.length(); i++, j = i) { |
| 63 | + char c = s.charAt(i); |
| 64 | + if (c == ')') { |
| 65 | + stack.pop(); |
| 66 | + } else if (c >= '0' && c <= '9' || c == '-') { |
| 67 | + while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') { |
| 68 | + i++; |
| 69 | + } |
| 70 | + TreeNode curr = new TreeNode(Integer.valueOf(s.substring(j, i + 1))); |
| 71 | + if (!stack.isEmpty()) { |
| 72 | + TreeNode parent = stack.peek(); |
| 73 | + if (parent.left != null) { |
| 74 | + parent.right = curr; |
| 75 | + } else { |
| 76 | + parent.left = curr; |
| 77 | + } |
| 78 | + } |
| 79 | + stack.push(curr); |
| 80 | + } |
43 | 81 | }
|
| 82 | + return stack.isEmpty() ? null : stack.peek(); |
44 | 83 | }
|
45 |
| - return cur; |
46 | 84 | }
|
47 | 85 |
|
48 | 86 | }
|
0 commit comments