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
46 changes: 46 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

/*
I perform DFS on the tree and at each node, I check if the value of every right child is greater than that of its parent and value of left child is smaller than that of it's parent.
I perform this logic checking recursively so that the value of all nodes in left subtree is smaller than the value of the root node and value of all nodes in right subtree is greater
than the value of the root 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
{
public bool IsValidBST(TreeNode root)
{
return Helper(root, Int64.MinValue, Int64.MaxValue);
}

public bool Helper(TreeNode root, long min, long max)
{
if (root == null)
{
return true;
}

if (root.val <= min || root.val >= max)
{
return false;
}

return Helper(root.left, min, root.val) && Helper(root.right, root.val, max);
}
}
61 changes: 61 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity : O(n)
// Space Complexity : O(n) = Space taken to build the hashmap
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

/*
I use the preorder list to keep track of values of root nodes (Preorder = Root -> Recursively traverse left subtree -> Recursively traverse right subtree) 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 int index;
private Dictionary<int, int> inorderLookup;

public TreeNode BuildTree(int[] preorder, int[] inorder)
{
index = 0;
inorderLookup = new();

for (int i = 0; i < inorder.Length; i++)
{
inorderLookup[inorder[i]] = i;
}

return Helper(0, preorder.Length - 1, preorder);
}

public TreeNode Helper(int start, int end, int[] preorder)
{
// Base condition
if (start > end)
{
return null;
}

int rootVal = preorder[index];
index += 1;
int inorderRootPosition = inorderLookup[rootVal];

TreeNode temp = new TreeNode(rootVal);

temp.left = Helper(start, inorderRootPosition - 1, preorder);
temp.right = Helper(inorderRootPosition + 1, end, preorder);

return temp;
}
}