diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..a0b141bd --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,62 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) to store the dictionary which contains the mapping for inorder array +// 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 + +/* +I use the postorder list to keep track of values of root nodes (Postorder = Recursively traverse left subtree -> Recursively traverse right subtree -> Visit root) and inorder list to obtain information about + what nodes belong to the left subtree of a given node and what nodes belong to the right subtree of a given node. +*/ + + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution +{ + private Dictionary inorderLookup; + private int index; + public TreeNode BuildTree(int[] inorder, int[] postorder) + { + inorderLookup = new(); + index = postorder.Length - 1; + + for (int i = 0; i < inorder.Length; i++) + { + inorderLookup[inorder[i]] = i; + } + + return Helper(0, inorder.Length - 1, postorder); + } + + public TreeNode Helper(int st, int end, int[] postorder) + { + if (st > end) + { + return null; + } + + int rootVal = postorder[index]; + index -= 1; + int rootIndex = inorderLookup[rootVal]; + TreeNode temp = new TreeNode(rootVal); + temp.right = Helper(rootIndex + 1, end, postorder); + temp.left = Helper(st, rootIndex - 1, postorder); + + return temp; + + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..ad0dde7d --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,55 @@ +// Time Complexity : O(n) +// Space Complexity : O(h) space taken up by the recursion stack +// 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 + +/* +I perform DFS on the tree, I use a global variable totalSum to keep track of sum so far. If root is null, I simply return from the helper method. I calculate sum as sum = sum * 10 + root.val. +If the current node is a lead node, I add sum to totalSum and return from the helper method. +*/ + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution +{ + private int totalSum; + public int SumNumbers(TreeNode root) + { + totalSum = 0; + Helper(root, 0); + return totalSum; + } + + private void Helper(TreeNode root, int sum) + { + if (root == null) + { + return; + } + + sum = sum * 10 + root.val; + + if (root.left == null && root.right == null) + { + totalSum += sum; + return; + } + + Helper(root.left, sum); + Helper(root.right, sum); + } +} \ No newline at end of file