Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions construct-binary-tree-from-inorder-and-postorder-traversal.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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;
}
}

61 changes: 61 additions & 0 deletions sum-root-to-leaf-numbers.java
Original file line number Diff line number Diff line change
@@ -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);
}
}