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
31 changes: 31 additions & 0 deletions BTinorderPostorder
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//O(n) TC
// O(n) SC
class Solution {
HashMap<Integer,Integer> hm ;
int postIdx;
public TreeNode buildTree(int[] inorder, int[] postorder) {
this.hm=new HashMap();
postIdx=postorder.length-1;
for(int i=0;i<postorder.length;i++){
hm.put(inorder[i],i);
}

return helper(postorder,0,postorder.length-1);

}
private TreeNode helper(int[] postorder,int st,int end){
if(st>end) return null;

int rootVal=postorder[postIdx];
int rootIdx=hm.get(rootVal);

postIdx--;

TreeNode root=new TreeNode(rootVal);
root.right=helper(postorder,rootIdx+1,end);
root.left=helper(postorder,st,rootIdx-1);
return root;


}
}
34 changes: 34 additions & 0 deletions rootToLeafSum
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//TC :- O(n)
//SC:- O(h)


class Solution {
int xsum = 0;

public int sumNumbers(TreeNode root) {
if (root == null)
return xsum;
helper(root, 0);
return xsum;

}

private void helper(TreeNode root, int sum) {
if (root == null) {

return;
}
sum=10 * sum + root.val;
if (root.left == null && root.right == null) {

xsum = xsum + sum;
}

helper(root.left, sum);


helper(root.right, sum);


}
}