From a149041b564df1ed024a4623bcd4a9dbb72b06d1 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Mon, 29 Jun 2026 21:37:19 -0500 Subject: [PATCH] Completed Trees 2 --- ConstructBTFromInOrderPostOrderTraversal.java | 56 +++++++++++++++++++ SumRootToLeaf.java | 43 ++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 ConstructBTFromInOrderPostOrderTraversal.java create mode 100644 SumRootToLeaf.java diff --git a/ConstructBTFromInOrderPostOrderTraversal.java b/ConstructBTFromInOrderPostOrderTraversal.java new file mode 100644 index 00000000..2042869a --- /dev/null +++ b/ConstructBTFromInOrderPostOrderTraversal.java @@ -0,0 +1,56 @@ +import java.util.HashMap; + +/** + * 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 { + /* + SC - O(n) + TC - O(n) + This solution reconstructs the binary tree using the inorder and postorder traversals. + A HashMap stores the index of each value in the inorder array for quick lookup. + Starting from the end of the postorder array, each value is taken as the root of the current subtree. + The root's position in the inorder array splits the tree into left and right subtrees. + Since the postorder array is processed backwards (Root → Right → Left), the right subtree is built + first, followed by the left subtree. The recursion continues until there are no nodes left in + the current inorder range. + */ + HashMap map; + int idx; + public TreeNode buildTree(int[] inorder, int[] postorder) { + this.map = new HashMap<>(); + for (int i = 0; i< inorder.length; i++) { + map.put(inorder[i], i); + } + this.idx = postorder.length - 1; + return helper(postorder, 0, postorder.length - 1); + } + + private TreeNode helper(int[] postorder, int start, int end) { + if (start > end) { + return null; + } + + int rootVal = postorder[this.idx]; + this.idx--; + int rootIdx = map.get(rootVal); + + TreeNode node = new TreeNode(rootVal); + node.right = helper(postorder, rootIdx + 1, end); + node.left = helper(postorder, start, rootIdx - 1); + + return node; + } +} \ No newline at end of file diff --git a/SumRootToLeaf.java b/SumRootToLeaf.java new file mode 100644 index 00000000..9db5753c --- /dev/null +++ b/SumRootToLeaf.java @@ -0,0 +1,43 @@ +/** + * 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 { + /* + TC - O(n) + SC - O(h) + + This solution usespreorder traversal to calculate the sum of all root-to-leaf numbers. Starting from the root, + we build the current number by multiplying the previous value by 10 and adding the current node's value. + When a leaf node is reached (a node with no left or right child), the complete number formed along that path + is added to the total sum. The recursion continues for both the left and right children, carrying the current + number along each path. After all root-to-leaf paths are explored, the total sum is returned + */ + int sum = 0; + public int sumNumbers(TreeNode root) { + helper(root, 0); + return sum; + } + + private void helper(TreeNode root, int curSum) { + if (root != null) { + curSum = curSum * 10 + root.val; + if (root.left == null && root.right == null) { + sum += curSum; + } + helper(root.left, curSum); + helper(root.right, curSum); + } + } +} \ No newline at end of file