From 775e7b7bc264c48e3c05709fce6597d4189a8600 Mon Sep 17 00:00:00 2001 From: dewann Date: Sun, 8 Feb 2026 10:28:13 -0800 Subject: [PATCH 1/2] Implement root-to-leaf sum calculation --- rootToLeafSum | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rootToLeafSum diff --git a/rootToLeafSum b/rootToLeafSum new file mode 100644 index 00000000..d4157d53 --- /dev/null +++ b/rootToLeafSum @@ -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); + + + } +} From 57f7814362a7a93ab8ab78f1f864cd2f33b97b20 Mon Sep 17 00:00:00 2001 From: dewann Date: Sun, 8 Feb 2026 10:31:21 -0800 Subject: [PATCH 2/2] Implement binary tree construction from inorder and postorder --- BTinorderPostorder | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 BTinorderPostorder diff --git a/BTinorderPostorder b/BTinorderPostorder new file mode 100644 index 00000000..b1652799 --- /dev/null +++ b/BTinorderPostorder @@ -0,0 +1,31 @@ +//O(n) TC +// O(n) SC +class Solution { + HashMap hm ; + int postIdx; + public TreeNode buildTree(int[] inorder, int[] postorder) { + this.hm=new HashMap(); + postIdx=postorder.length-1; + for(int i=0;iend) 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; + + + } +}