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
62 changes: 62 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -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<int, int> 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;

}
}
55 changes: 55 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}