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
51 changes: 51 additions & 0 deletions ConstructBinaryTreeFromIn-PostOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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;
}
}
47 changes: 47 additions & 0 deletions SumrootToLeafNumbers.java
Original file line number Diff line number Diff line change
@@ -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);
}
}