Advertisement
1988coder

536. Construct Binary Tree from String

Nov 28th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. /*
  11. Recursive Solution (This solution makes sure the recursion starts at the value.)
  12. This solution takes care of "()" (left child is null).
  13.  
  14. Time Complexity: O(N)
  15. Space Complexity: O(H)
  16. N = Length of the input string
  17. H = Height of the output tree.
  18. */
  19. class Solution {
  20.     public TreeNode str2tree(String s) {
  21.         if (s == null || s.length() == 0) {
  22.             return null;
  23.         }
  24.         return str2treeHelper(s, new int[1]);
  25.     }
  26.    
  27.     private TreeNode str2treeHelper(String s, int[] index) {
  28.         if (index[0] >= s.length()) {
  29.             return null;
  30.         }
  31.        
  32.         int start = index[0];
  33.         while (index[0] < s.length() && s.charAt(index[0]) != '(' && s.charAt(index[0]) != ')') {
  34.             index[0]++;
  35.         }
  36.        
  37.         TreeNode node = new TreeNode(Integer.parseInt(s.substring(start, index[0])));
  38.        
  39.         if (index[0] < s.length() && s.charAt(index[0]) == '(') {
  40.             index[0]++;
  41.             node.left = str2treeHelper(s, index);
  42.             if (index[0] < s.length() && s.charAt(index[0]) == '(') {
  43.                 index[0]++;
  44.                 node.right = str2treeHelper(s, index);
  45.             }
  46.         }
  47.        
  48.         index[0]++;
  49.         return node;
  50.     }
  51. }
  52.  
  53.  
  54. /*
  55. Recursive Solution (Adding padding to the string. Making sure the recursion always start at open bracket)
  56. This solution takes care of "()" (left child is null).
  57.  
  58. Time Complexity: O(N)
  59. Space Complexity: O(H)
  60. N = Length of the input string
  61. H = Height of the output tree.
  62. */
  63. class Solution {
  64.     public TreeNode str2tree(String s) {
  65.         if (s == null || s.length() == 0) {
  66.             return null;
  67.         }
  68.         return str2treeHelper("(" + s + ")", new int[1]);
  69.     }
  70.    
  71.     private TreeNode str2treeHelper(String s, int[] index) {
  72.         int start = index[0]+1;
  73.         int end = start+1;
  74.         while (end < s.length() && Character.isDigit(s.charAt(end))) {
  75.             end++;
  76.         }
  77.         index[0] = end;
  78.        
  79.         TreeNode node = new TreeNode(Integer.parseInt(s.substring(start, end)));
  80.        
  81.         if (s.charAt(index[0]) == '(') {
  82.             node.left = str2treeHelper(s, index);
  83.             if (s.charAt(index[0]) == '(') {
  84.                 node.right = str2treeHelper(s, index);
  85.             }
  86.         }
  87.        
  88.         index[0]++;
  89.         return node;
  90.     }
  91. }
  92.  
  93.  
  94. /*
  95. Iterative Solution
  96. This solution constructs the left child node of the parent first if it exists
  97.  
  98. Time Complexity: O(N)
  99. Space Complexity: O(H)
  100. N = Length of the input string
  101. H = Height of the output tree.
  102. */
  103. class Solution {
  104.     public TreeNode str2tree(String s) {
  105.         if (s == null || s.length() == 0) {
  106.             return null;
  107.         }
  108.        
  109.         Stack<TreeNode> stack = new Stack();
  110.         int i = 0;
  111.         while (i < s.length()) {
  112.             if (s.charAt(i) == ')' && !stack.isEmpty()) {
  113.                 stack.pop();
  114.                 i++;
  115.                 continue;
  116.             }
  117.             if (s.charAt(i) == '(') {
  118.                 i++;
  119.             }
  120.             int start = i;
  121.             while(i < s.length() && (s.charAt(i) == '-' || Character.isDigit(s.charAt(i)))) {
  122.                 i++;
  123.             }
  124.             TreeNode node = new TreeNode(Integer.parseInt(s.substring(start, i)));
  125.             if (!stack.isEmpty()) {
  126.                 TreeNode parent = stack.peek();
  127.                 if (parent.left == null) {
  128.                     parent.left = node;
  129.                 } else {
  130.                     parent.right = node;
  131.                 }
  132.             }
  133.             stack.push(node);
  134.         }
  135.        
  136.         return stack.isEmpty() ? null : stack.peek();
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement