diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..6d68a85a --- /dev/null +++ b/Problem1.cs @@ -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); + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..96c146be --- /dev/null +++ b/Problem2.cs @@ -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 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; + } +} \ No newline at end of file