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
56 changes: 56 additions & 0 deletions ConstructBTFromInOrderPostOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}
43 changes: 43 additions & 0 deletions SumRootToLeaf.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}