Skip to content

Commit 464603a

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent eb0d465 commit 464603a

3 files changed

+159
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@
411411

412412
[234. Palindrome Linked List](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/234.%20Palindrome%20Linked%20List.md)
413413

414+
[235. Lowest Common Ancestor of a Binary Search Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/235.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree.md)
415+
416+
[236. Lowest Common Ancestor of a Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.md)
417+
414418
## Algorithm(4th_Edition)
415419
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
416420
All java realization codes are placed in different packages.

leetcode/235. Lowest Common Ancestor of a Binary Search Tree.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,62 @@ class Solution {
8787
return root;
8888
}
8989
}
90-
```
90+
```
91+
92+
### 二刷
93+
1. Same thing as Method1
94+
```Java
95+
/**
96+
* Definition for a binary tree node.
97+
* public class TreeNode {
98+
* int val;
99+
* TreeNode left;
100+
* TreeNode right;
101+
* TreeNode(int x) { val = x; }
102+
* }
103+
*/
104+
class Solution {
105+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
106+
if(p == root || q == root) return root;
107+
boolean pInLeft = isChild(root.left, p);
108+
boolean pInRight = isChild(root.right, p);
109+
boolean qInLeft = isChild(root.left, q);
110+
boolean qInRight = isChild(root.right, q);
111+
if(pInLeft && qInLeft){
112+
return lowestCommonAncestor(root.left, p, q);
113+
}else if(pInRight && qInRight){
114+
return lowestCommonAncestor(root.right, p, q);
115+
}else{
116+
return root;
117+
}
118+
}
119+
120+
private boolean isChild(TreeNode node, TreeNode p){
121+
if(node == null) return false;
122+
if(node == p) return true;
123+
return isChild(node.left, p) || isChild(node.right, p);
124+
}
125+
}
126+
```
127+
128+
2. Different from the previous method, we can use the property of BST and check isChild by just comparing the value, and even can check if current node is in left or right child node of current node.
129+
```Java
130+
/**
131+
* Definition for a binary tree node.
132+
* public class TreeNode {
133+
* int val;
134+
* TreeNode left;
135+
* TreeNode right;
136+
* TreeNode(int x) { val = x; }
137+
* }
138+
*/
139+
class Solution {
140+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
141+
if(root == null) return null;
142+
if(root.val == p.val || root.val == q.val) return root;
143+
else if(p.val < root.val && q.val < root.val) return lowestCommonAncestor(root.left, p, q);
144+
else if(p.val > root.val && q.val > root.val) return lowestCommonAncestor(root.right, p, q);
145+
else return root;
146+
}
147+
}
148+
```

leetcode/236. Lowest Common Ancestor of a Binary Tree.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,99 @@ class Solution {
8585
return null;
8686
}
8787
}
88-
```
88+
```
89+
90+
### 二刷
91+
1. Same as previous quesiton, I just copy and paste.
92+
```Java
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode(int x) { val = x; }
100+
* }
101+
*/
102+
class Solution {
103+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
104+
if(root == null) return null;
105+
if(root == q || root == p) return root;
106+
boolean pinLeft = contains(root.left, p);
107+
boolean pinRight = contains(root.right, p);
108+
boolean qinRight = contains(root.right, q);
109+
boolean qinLeft = contains(root.left, q)
110+
if(pinLeft && qinRight) return root;
111+
else if(pinRight && qinLeft) return root;
112+
else if(pinLeft && qinLeft) return lowestCommonAncestor(root.left, p, q);
113+
else if(pinRight && qinRight) return lowestCommonAncestor(root.right, p, q);
114+
return null;
115+
}
116+
private boolean contains(TreeNode root, TreeNode node){
117+
if(root == null) return false;
118+
if(root == node) return true;
119+
return contains(root.left, node) || contains(root.right, node);
120+
}
121+
}
122+
```
123+
124+
2. I modified the result a little bit and get a result.
125+
```Java
126+
/**
127+
* Definition for a binary tree node.
128+
* public class TreeNode {
129+
* int val;
130+
* TreeNode left;
131+
* TreeNode right;
132+
* TreeNode(int x) { val = x; }
133+
* }
134+
*/
135+
class Solution {
136+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
137+
if(p == root || q == root) return root;
138+
boolean pInLeft = isChild(root.left, p);
139+
boolean qInRight = isChild(root.right, q);
140+
if(pInLeft && qInRight || !pInLeft && !qInRight) return root;
141+
else if(!pInLeft && qInRight){
142+
return lowestCommonAncestor(root.right, p, q);
143+
}else{
144+
return lowestCommonAncestor(root.left, p, q);
145+
}
146+
}
147+
148+
private boolean isChild(TreeNode node, TreeNode p){
149+
if(node == null) return false;
150+
if(node == p) return true;
151+
return isChild(node.left, p) || isChild(node.right, p);
152+
}
153+
}
154+
```
155+
156+
3. Method 3, it is using divide and conquer method.
157+
* We first find their common ancestor in left child. Result is A.
158+
* Then we find their common ancestor in right child. Result is B.
159+
* if A and B are both not null, it means current node is their closest common ancestor.
160+
* if one of them is not null, the other one is their closest common ancestor.
161+
```Java
162+
/**
163+
* Definition for a binary tree node.
164+
* public class TreeNode {
165+
* int val;
166+
* TreeNode left;
167+
* TreeNode right;
168+
* TreeNode(int x) { val = x; }
169+
* }
170+
*/
171+
class Solution {
172+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
173+
if(root == p || root == q || root == null) return root;
174+
TreeNode left = lowestCommonAncestor(root.left, p ,q);
175+
TreeNode right = lowestCommonAncestor(root.right, p ,q);
176+
if(left != null && right != null){
177+
return root;
178+
}else if(left != null) return left;
179+
else if(right != null) return right;
180+
return null;
181+
}
182+
}
183+
```

0 commit comments

Comments
 (0)