Advertisement
1988coder

129. Sum Root to Leaf Numbers

Oct 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 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 DFS Solution.
  12. Time Complexity = O(n). All nodes will be visited once.
  13. Space Complexity = O(n). Recursion stack can grow up to n size if the tree is skewed.
  14. */
  15. class Solution {
  16.     public int sumNumbers(TreeNode root) {
  17.         if (root == null) {
  18.             return 0;
  19.         }
  20.         return sumNumbersHelper(root, 0);
  21.     }
  22.    
  23.     public int sumNumbersHelper(TreeNode node, int currVal) {
  24.         if (node == null) {
  25.             return 0;
  26.         }
  27.         currVal = currVal * 10 + node.val;
  28.         if (node.left == null && node.right == null) {
  29.             return currVal;
  30.         }
  31.         return sumNumbersHelper(node.left, currVal) + sumNumbersHelper(node.right, currVal);
  32.     }
  33. }
  34.  
  35. /**
  36. Recursive Post Order Traversal
  37. Time Complexity: O(n) -> Each node is visited twice.
  38. Space Complexity: O(n) -> Stack can grow upto all nodes if the tree is skewed.
  39. */
  40. class Solution {
  41.     public int sumNumbers(TreeNode root) {
  42.         if (root == null) {
  43.             return 0;
  44.         }
  45.        
  46.         Stack<TreeNode> stack = new Stack<>();
  47.         int result = 0;
  48.         int num = 0;
  49.         TreeNode cur = root;
  50.         TreeNode pre = null;
  51.        
  52.         while (cur != null || !stack.isEmpty()) {
  53.             while (cur != null) {
  54.                 stack.push(cur);
  55.                 num = num * 10 + cur.val;
  56.                 cur = cur.left;
  57.             }
  58.             cur = stack.peek();
  59.             if (cur.right != null && pre != cur.right) {
  60.                 cur = cur.right;
  61.                 continue;
  62.             }
  63.             if (cur.left == null & cur.right == null) {
  64.                 result += num;
  65.             }
  66.             pre = stack.pop();
  67.             num /= 10;
  68.             cur = null;
  69.         }
  70.         return result;
  71.     }
  72. }
  73.  
  74. /**
  75. DFS + Stack
  76. Time Complexity: O(n)
  77. Space Complexity: O(n)
  78. */
  79. import javafx.util.Pair;
  80. class Solution {
  81.     public int sumNumbers(TreeNode root) {
  82.         if (root == null) {
  83.             return 0;
  84.         }
  85.         Stack<Pair<TreeNode, Integer>> stack = new Stack<>();
  86.         stack.push(new Pair<TreeNode, Integer>(root, root.val));
  87.         int result = 0;
  88.        
  89.         while (!stack.isEmpty()) {
  90.             Pair<TreeNode, Integer> pair = stack.pop();
  91.             if (pair.getKey().left == null && pair.getKey().right == null) {
  92.                 result += pair.getValue();
  93.             }
  94.             if (pair.getKey().left != null) {
  95.                 TreeNode left = pair.getKey().left;
  96.                 int leftVal = pair.getValue() * 10 + left.val;
  97.                 stack.push(new Pair<TreeNode, Integer>(left, leftVal));
  98.             }
  99.             if (pair.getKey().right != null) {
  100.                 TreeNode right = pair.getKey().right;
  101.                 int rightVal = pair.getValue() * 10 + right.val;
  102.                 stack.push(new Pair<TreeNode, Integer>(right, rightVal));
  103.             }
  104.         }
  105.         return result;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement