diff --git a/ConstructBinaryTreeFromIn-PostOrderTraversal.java b/ConstructBinaryTreeFromIn-PostOrderTraversal.java new file mode 100644 index 00000000..03c13e7b --- /dev/null +++ b/ConstructBinaryTreeFromIn-PostOrderTraversal.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Maintain a map to store the inorder array elements and their indices to understand the left and right elements +of root, once we identify the root from postorder array. We can have a helper method to get root element using +a pointer for each recursion and leverage that root element and map to find root's index. All the values before +that index in inorder array would be left and after the index is right. This way, we can construct the whole tree. + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + int index; + public TreeNode buildTree(int[] inorder, int[] postorder) { + this.index = postorder.length - 1; + Map map = new HashMap<>(); + for(int i = 0 ; i < inorder.length ; i++) + map.put(inorder[i] , i); + return helper(postorder, 0, postorder.length - 1, map); + } + + private TreeNode helper(int[] postorder, int start, int end, Map map) { + if(start > end) + return null; + int rootVal = postorder[index]; + index--; + + int rootIndex = map.get(rootVal); + TreeNode root = new TreeNode(rootVal); + + root.right = helper(postorder, rootIndex + 1, end, map); + root.left = helper(postorder, start, rootIndex - 1, map); + return root; + } +} \ No newline at end of file diff --git a/SumrootToLeafNumbers.java b/SumrootToLeafNumbers.java new file mode 100644 index 00000000..a9b6049f --- /dev/null +++ b/SumrootToLeafNumbers.java @@ -0,0 +1,47 @@ +// Time Complexity : O(n) +// Space Complexity : O(H) - height of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Have a helper method to calculate the overall sum. We recursively call root's left and right by making sure +that both are not null. If we hit leaf nodes, it means we need to store the result for that branch. To form +current number, we multiply the digit with 10 and add root at each recursion. To end the recursion, +we use a base case to check if root is null. + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + private int result; + public int sumNumbers(TreeNode root) { + this.result = 0; + calculateSum(root, 0); + return result; + } + + private void calculateSum(TreeNode root, int currNum) { + if(root == null) + return; + + currNum = (currNum * 10) + root.val; + + if(root.left == null && root.right == null) + result += currNum; + calculateSum(root.left, currNum); + calculateSum(root.right, currNum); + } +} \ No newline at end of file