Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1dbcf98

Browse files
committed
update
1 parent 9ba34d7 commit 1dbcf98

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Grind169.BinarySearch;
2+
3+
public class BinarySearch_704 {
4+
public static void main(String[] args) {
5+
int nums[]={-1,0,3,5,9,12};
6+
int target=9;
7+
8+
System.out.println(search(nums, target));
9+
}
10+
11+
public static int search(int[] nums, int target) {
12+
int low=0;
13+
int high= nums.length-1;
14+
15+
while(low <= high){
16+
int mid=(low+high)/2;
17+
if(nums[mid] > target){
18+
high=mid-1;
19+
}else if(nums[mid] == target){
20+
return mid;
21+
}else{
22+
low=mid+1;
23+
}
24+
25+
}
26+
27+
return -1;
28+
}
29+
}

LeetCode/Grind169/BinaryTrees/InvertBinaryTree_226.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package Grind169.BinaryTrees;
22

33
public class InvertBinaryTree_226 {
4-
54

65
class TreeNode {
76
int val;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Grind169.BinaryTrees;
2+
3+
public class SameTree_100 {
4+
class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
TreeNode() {}
9+
TreeNode(int val) { this.val = val; }
10+
TreeNode(int val, TreeNode left, TreeNode right) {
11+
this.val = val;
12+
this.left = left;
13+
this.right = right;
14+
}
15+
}
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
public boolean isSameTree(TreeNode p, TreeNode q) {
22+
// If both root nodes p and q are null tree is true
23+
if(p==null && q== null){
24+
return true;
25+
}
26+
27+
//if one root is not null and the other root is null they are not the same tree thus false
28+
if(p== null && q != null || p!=null && q==null){
29+
return false;
30+
}
31+
32+
// if both nodes do not have the same value then return false
33+
if(p.val != q.val){
34+
return false;
35+
}
36+
// recursive call for both left nodes of each root node and right node of each root
37+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Grind169.BinaryTrees;
2+
3+
public class SubtreeOfAnotherTree_572 {
4+
class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
TreeNode() {}
9+
TreeNode(int val) { this.val = val; }
10+
TreeNode(int val, TreeNode left, TreeNode right) {
11+
this.val = val;
12+
this.left = left;
13+
this.right = right;
14+
}
15+
}
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
/**
22+
* Time O(n)
23+
* Space O(h)
24+
*/
25+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
26+
if (root == null) return false;
27+
if (dfs(root, subRoot)) return true;
28+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
29+
}
30+
31+
/**
32+
* Time O(n)
33+
* Space O(h)
34+
* - Each element costs constant space O(1).
35+
* And the size of the stack is exactly the depth of DFS.
36+
* So in the worst case, it costs O(h) to maintain the system stack,
37+
* where h is the maximum depth of DFS.
38+
*/
39+
public boolean dfs(TreeNode root, TreeNode subRoot) {
40+
if (root == null && subRoot == null) return true;
41+
if (root == null || subRoot == null) return false;
42+
if (root.val != subRoot.val) return false;
43+
return dfs(root.left, subRoot.left) && dfs(root.right, subRoot.right);
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Grind169.BinaryTrees;
2+
3+
public class SymmetricTree_101 {
4+
class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
TreeNode() {}
9+
TreeNode(int val) { this.val = val; }
10+
TreeNode(int val, TreeNode left, TreeNode right) {
11+
this.val = val;
12+
this.left = left;
13+
this.right = right;
14+
}
15+
}
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
//helper method
22+
public boolean isSymmetric(TreeNode root) {
23+
//if root is null than there is no tree hence not symmetric
24+
if(root == null){ return false;}
25+
// check whether next nodes are equal
26+
return checkSymmetric(root.left, root.right);
27+
}
28+
29+
public boolean checkSymmetric(TreeNode leftParent, TreeNode rightParent){
30+
//checks whether nodes are both null or one is null anf other not
31+
if(leftParent == null && rightParent == null){return true;}
32+
if(leftParent == null || rightParent == null){return false;}
33+
//check whether the value of each node is not equal
34+
if(leftParent.val != rightParent.val){return false;}
35+
36+
return checkSymmetric(leftParent.left, rightParent.right) && checkSymmetric(leftParent.right, rightParent.left);
37+
38+
}
39+
}

0 commit comments

Comments
 (0)