From 878385ebbb0dfd7fd56fc9375a745958c5fe68cd Mon Sep 17 00:00:00 2001 From: tmujje Date: Fri, 14 May 2021 01:00:12 -0400 Subject: [PATCH 1/2] Trees-1 Submission --- InOrderPreOrderTraversal.java | 33 +++++++++++++++++++++++++++++++++ ValidateBST.java | 24 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 InOrderPreOrderTraversal.java create mode 100644 ValidateBST.java diff --git a/InOrderPreOrderTraversal.java b/InOrderPreOrderTraversal.java new file mode 100644 index 00000000..1b2df2f0 --- /dev/null +++ b/InOrderPreOrderTraversal.java @@ -0,0 +1,33 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n power 2) we are iterating through the inorder array every time through our recursive calls. +//Space Complexity: O(1) since we are not taking any extra space +class InOrderPreOrderTraversal { + public TreeNode buildTree(int[] preorder, int[] inorder) + { + //PreOrder - root, left, right + //InOrder - left, root, right + if(preorder.length == 0) return null; + int idx = -1; + int rootVal = preorder[0]; // first element in preOrder is nothing but root + TreeNode root = new TreeNode(rootVal); + + //Finding the index of the root value in the inorder array + for(int i = 0; i < inorder.length; i++){ + if(inorder[i] == rootVal){ + idx = i; + break; + } + } + + int[] preLeft = Arrays.copyOfRange(preorder, 1, idx+1); + int[] preRight = Arrays.copyOfRange(preorder, idx+1, preorder.length); + int[] inLeft = Arrays.copyOfRange(inorder, 0, idx); + int[] inRight = Arrays.copyOfRange(inorder, idx+1, inorder.length); + + root.left = buildTree(preLeft, inLeft); + root.right = buildTree(preRight, inRight); + return root; + } +} diff --git a/ValidateBST.java b/ValidateBST.java new file mode 100644 index 00000000..15826b11 --- /dev/null +++ b/ValidateBST.java @@ -0,0 +1,24 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are traversing across all the nodes in the tree in worst case scenario +//Space Complexity: O(1) since we are not taking any extra space + + class Solution { + + TreeNode prev = null; + + public boolean isValidBST(TreeNode root) + { + //base condition + if(root == null) return true; + + //logic + if(isValidBST(root.left) == false) return false; + //Since traversing through the BST results in a sorted order, + //if the current prev root value is greater than current root val, then it is not a BST + if(prev != null && prev.val >= root.val) return false; + prev = root; + return isValidBST(root.right); + } +} \ No newline at end of file From d646bf50a9741dea0a9dc296d6a834e64615aa24 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Fri, 21 Aug 2026 00:03:29 -0400 Subject: [PATCH 2/2] Complete Trees-1 assignment --- ...=> BinaryTreePreOrderInOrderTraversal.java | 22 ++++----- ValidBST.java | 46 +++++++++++++++++++ ValidateBST.java | 24 ---------- 3 files changed, 56 insertions(+), 36 deletions(-) rename InOrderPreOrderTraversal.java => BinaryTreePreOrderInOrderTraversal.java (62%) create mode 100644 ValidBST.java delete mode 100644 ValidateBST.java diff --git a/InOrderPreOrderTraversal.java b/BinaryTreePreOrderInOrderTraversal.java similarity index 62% rename from InOrderPreOrderTraversal.java rename to BinaryTreePreOrderInOrderTraversal.java index 1b2df2f0..91878aa7 100644 --- a/InOrderPreOrderTraversal.java +++ b/BinaryTreePreOrderInOrderTraversal.java @@ -1,10 +1,8 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No -//Time Complexity : O(n power 2) we are iterating through the inorder array every time through our recursive calls. -//Space Complexity: O(1) since we are not taking any extra space -class InOrderPreOrderTraversal { - public TreeNode buildTree(int[] preorder, int[] inorder) +//Time Complexity : O(n2) +//Space Complexity: O(1) +class BinaryTreePreOrderInOrderTraversal { + public TreeNode buildTree(int[] preorder, int[] inorder) { //PreOrder - root, left, right //InOrder - left, root, right @@ -12,7 +10,7 @@ public TreeNode buildTree(int[] preorder, int[] inorder) int idx = -1; int rootVal = preorder[0]; // first element in preOrder is nothing but root TreeNode root = new TreeNode(rootVal); - + //Finding the index of the root value in the inorder array for(int i = 0; i < inorder.length; i++){ if(inorder[i] == rootVal){ @@ -20,14 +18,14 @@ public TreeNode buildTree(int[] preorder, int[] inorder) break; } } - - int[] preLeft = Arrays.copyOfRange(preorder, 1, idx+1); + + int[] preLeft = Arrays.copyOfRange(preorder, 1, idx+1); int[] preRight = Arrays.copyOfRange(preorder, idx+1, preorder.length); int[] inLeft = Arrays.copyOfRange(inorder, 0, idx); int[] inRight = Arrays.copyOfRange(inorder, idx+1, inorder.length); - + root.left = buildTree(preLeft, inLeft); root.right = buildTree(preRight, inRight); - return root; + return root; } -} +} \ No newline at end of file diff --git a/ValidBST.java b/ValidBST.java new file mode 100644 index 00000000..b95a3042 --- /dev/null +++ b/ValidBST.java @@ -0,0 +1,46 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + +//Time Complexity : O(n) +//Space Complexity: O(h) for skewed where 'h' is the height of the tree +//Space Complexity: O(log n) for non-skewed +class ValidBST { + + // Initialize global variables + boolean isValidBST = true; + TreeNode prev = null; + + public boolean isValidBST(TreeNode root) { + helper(root); + return isValidBST; + } + + private void helper(TreeNode root) + { + //Base case + if(root == null) return; + + helper(root.left); + + // Check if current root value is less than or equal to previous root value + if (prev != null && prev.val >= root.val){ + isValidBST = false; + } + prev = root; + + helper(root.right); + } +} \ No newline at end of file diff --git a/ValidateBST.java b/ValidateBST.java deleted file mode 100644 index 15826b11..00000000 --- a/ValidateBST.java +++ /dev/null @@ -1,24 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are traversing across all the nodes in the tree in worst case scenario -//Space Complexity: O(1) since we are not taking any extra space - - class Solution { - - TreeNode prev = null; - - public boolean isValidBST(TreeNode root) - { - //base condition - if(root == null) return true; - - //logic - if(isValidBST(root.left) == false) return false; - //Since traversing through the BST results in a sorted order, - //if the current prev root value is greater than current root val, then it is not a BST - if(prev != null && prev.val >= root.val) return false; - prev = root; - return isValidBST(root.right); - } -} \ No newline at end of file