From 2f38ab28df82dc3fd69a61fca4410c3e263bb83c Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Fri, 30 Jan 2026 13:38:42 -0800 Subject: [PATCH] Adding Trees-2 solutions --- ...-from-inorder-and-postorder-traversal.java | 30 +++++++++ sum-root-to-leaf-numbers.java | 61 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 construct-binary-tree-from-inorder-and-postorder-traversal.java create mode 100644 sum-root-to-leaf-numbers.java diff --git a/construct-binary-tree-from-inorder-and-postorder-traversal.java b/construct-binary-tree-from-inorder-and-postorder-traversal.java new file mode 100644 index 00000000..da2ce15f --- /dev/null +++ b/construct-binary-tree-from-inorder-and-postorder-traversal.java @@ -0,0 +1,30 @@ + // TimeComplexity: O(n) + // SpaceComplexity: O(n) + // Explanation: I am building the binary tree recursively by taking the last element of the current postorder range as the root, because postorder visits nodes in left → right → root order. Using a HashMap of inorder values, I find the root’s position in the inorder array, which tells me the boundaries of the left and right subtrees. I first recursively construct the right subtree, then the left subtree, updating the postorder index as I go, until the entire tree is reconstructed. +class Solution { + int postIndex; + public TreeNode buildTree(int[] inorder, int[] postorder) { + postIndex = postorder.length-1; + Map inorderMap = new HashMap<>(); + for(int i=0; i < inorder.length; i++){ + inorderMap.put(inorder[i], i); + } + + return helper(postorder, inorderMap, 0 , inorder.length-1); + } + + + private TreeNode helper(int[] postorder, Map inorderMap, int inleft, int inright) { + if(inleft>inright) return null; + TreeNode root = new TreeNode(postorder[postIndex]); + postIndex--; + + + int rootidx = inorderMap.get(root.val); + root.right = helper(postorder, inorderMap,rootidx+1, inright); + root.left = helper(postorder, inorderMap,inleft, rootidx-1); + + return root; + } +} + diff --git a/sum-root-to-leaf-numbers.java b/sum-root-to-leaf-numbers.java new file mode 100644 index 00000000..fd4ff5d8 --- /dev/null +++ b/sum-root-to-leaf-numbers.java @@ -0,0 +1,61 @@ +// Solution -1 + +//TimeComplexity: O(n) n is number of nodes +//SpaceComplexity: O(h) h is height of the tree +// Explanation for both solutions : I am recursively traversing the binary tree to compute the sum of all numbers formed from root-to-leaf paths. +// At each node, I multiply the current value by 10 and add the node’s value to form the number along that path. +// If the node is a leaf, I either return the current number (in the first approach) or add it to a global sum variable (in the second approach). +// The recursion continues for left and right children until all paths are processed, and the total sum of all root-to-leaf numbers is computed. + +class Solution { + public int sumNumbers(TreeNode root) { + + return helper(root,0); + + + } + + private int helper (TreeNode root, int curr) { + if(root == null) { + return 0; + } + curr = curr*10 +root.val; + if(root.left == null && root.right == null){ + return curr; + + } + int left = helper(root.left, curr); + + int right = helper(root.right, curr); + return left+right; + } +} + +// Solution-2 + +//TimeComplexity: O(n) n is number of nodes +//SpaceComplexity: O(h) h is height of the tree +class Solution { + int sum = 0; + public int sumNumbers(TreeNode root) { + + helper(root,0); + return sum; + + } + + private void helper (TreeNode root, int curr) { + if(root == null) { + return; + } + curr = curr*10 +root.val; + if(root.left == null && root.right == null){ + sum+=curr; + + } + + helper(root.left, curr); + + helper(root.right, curr); + } +} \ No newline at end of file