Skip to content

Commit 94c4149

Browse files
committed
feat: update solutions to lcof and leetcode problem
1 parent e20c04b commit 94c4149

File tree

11 files changed

+226
-41
lines changed

11 files changed

+226
-41
lines changed

lcof/面试题68 - II. 二叉树的最近公共祖先/README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ class Solution:
7070
return root
7171
left = self.lowestCommonAncestor(root.left, p, q)
7272
right = self.lowestCommonAncestor(root.right, p, q)
73-
return right if left is None else (left if right is None else root)
73+
if left is None:
74+
return right
75+
if right is None:
76+
return left
77+
return root
7478
```
7579

7680
### **Java**
@@ -89,12 +93,12 @@ class Solution:
8993
*/
9094
class Solution {
9195
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
92-
if (root == null || root == p || root == q) {
93-
return root;
94-
}
96+
if (root == null || root == p || root == q) return root;
9597
TreeNode left = lowestCommonAncestor(root.left, p, q);
9698
TreeNode right = lowestCommonAncestor(root.right, p, q);
97-
return left == null ? right : (right == null ? left : root);
99+
if (left == null) return right;
100+
if (right == null) return left;
101+
return root;
98102
}
99103
}
100104
```
@@ -116,14 +120,12 @@ class Solution {
116120
* @return {TreeNode}
117121
*/
118122
var lowestCommonAncestor = function (root, p, q) {
119-
if (!root) return null;
120-
if (root === p || root === q) return root;
121-
let left = lowestCommonAncestor(root.left, p, q);
122-
let right = lowestCommonAncestor(root.right, p, q);
123-
if (left && right) return root;
124-
if (left) return left;
125-
if (right) return right;
126-
return null;
123+
if (!root || root == p || root == q) return root;
124+
const left = lowestCommonAncestor(root.left, p, q);
125+
const right = lowestCommonAncestor(root.right, p, q);
126+
if (!left) return right;
127+
if (!right) return left;
128+
return root;
127129
};
128130
```
129131

lcof/面试题68 - II. 二叉树的最近公共祖先/Solution.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
class Solution {
1111
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12-
if (root == null || root == p || root == q) {
13-
return root;
14-
}
12+
if (root == null || root == p || root == q) return root;
1513
TreeNode left = lowestCommonAncestor(root.left, p, q);
1614
TreeNode right = lowestCommonAncestor(root.right, p, q);
17-
return left == null ? right : (right == null ? left : root);
15+
if (left == null) return right;
16+
if (right == null) return left;
17+
return root;
1818
}
1919
}

lcof/面试题68 - II. 二叉树的最近公共祖先/Solution.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
* @return {TreeNode}
1313
*/
1414
var lowestCommonAncestor = function (root, p, q) {
15-
if (!root) return null;
16-
if (root === p || root === q) return root;
17-
let left = lowestCommonAncestor(root.left, p, q);
18-
let right = lowestCommonAncestor(root.right, p, q);
19-
if (left && right) return root;
20-
if (left) return left;
21-
if (right) return right;
22-
return null;
15+
if (!root || root == p || root == q) return root;
16+
const left = lowestCommonAncestor(root.left, p, q);
17+
const right = lowestCommonAncestor(root.right, p, q);
18+
if (!left) return right;
19+
if (!right) return left;
20+
return root;
2321
};

lcof/面试题68 - II. 二叉树的最近公共祖先/Solution.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> Tree
1111
return root
1212
left = self.lowestCommonAncestor(root.left, p, q)
1313
right = self.lowestCommonAncestor(root.right, p, q)
14-
return right if left is None else (left if right is None else root)
14+
if left is None:
15+
return right
16+
if right is None:
17+
return left
18+
return root

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

50+
迭代:
51+
5052
```python
5153
# Definition for a binary tree node.
5254
# class TreeNode:
@@ -68,10 +70,33 @@ class Solution:
6870
return root
6971
```
7072

73+
递归:
74+
75+
```python
76+
# Definition for a binary tree node.
77+
# class TreeNode:
78+
# def __init__(self, x):
79+
# self.val = x
80+
# self.left = None
81+
# self.right = None
82+
83+
class Solution:
84+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
85+
if root is None:
86+
return None
87+
if root.val < p.val and root.val < q.val:
88+
return self.lowestCommonAncestor(root.right, p, q)
89+
if root.val > p.val and root.val > q.val:
90+
return self.lowestCommonAncestor(root.left, p, q)
91+
return root
92+
```
93+
7194
### **Java**
7295

7396
<!-- 这里可写当前语言的特殊实现逻辑 -->
7497

98+
迭代:
99+
75100
```java
76101
/**
77102
* Definition for a binary tree node.
@@ -102,6 +127,29 @@ class Solution {
102127
}
103128
```
104129

130+
递归:
131+
132+
```java
133+
/**
134+
* Definition for a binary tree node.
135+
* public class TreeNode {
136+
* int val;
137+
* TreeNode left;
138+
* TreeNode right;
139+
* TreeNode(int x) { val = x; }
140+
* }
141+
*/
142+
143+
class Solution {
144+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
145+
if (root == null) return null;
146+
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
147+
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
148+
return root;
149+
}
150+
}
151+
```
152+
105153
### **Go**
106154

107155
```go

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README_EN.md

+48
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
### **Python3**
5353

54+
Iterative:
55+
5456
```python
5557
# Definition for a binary tree node.
5658
# class TreeNode:
@@ -72,8 +74,31 @@ class Solution:
7274
return root
7375
```
7476

77+
Recursive:
78+
79+
```python
80+
# Definition for a binary tree node.
81+
# class TreeNode:
82+
# def __init__(self, x):
83+
# self.val = x
84+
# self.left = None
85+
# self.right = None
86+
87+
class Solution:
88+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
89+
if root is None:
90+
return None
91+
if root.val < p.val and root.val < q.val:
92+
return self.lowestCommonAncestor(root.right, p, q)
93+
if root.val > p.val and root.val > q.val:
94+
return self.lowestCommonAncestor(root.left, p, q)
95+
return root
96+
```
97+
7598
### **Java**
7699

100+
Iterative:
101+
77102
```java
78103
/**
79104
* Definition for a binary tree node.
@@ -104,6 +129,29 @@ class Solution {
104129
}
105130
```
106131

132+
Recursive:
133+
134+
```java
135+
/**
136+
* Definition for a binary tree node.
137+
* public class TreeNode {
138+
* int val;
139+
* TreeNode left;
140+
* TreeNode right;
141+
* TreeNode(int x) { val = x; }
142+
* }
143+
*/
144+
145+
class Solution {
146+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
147+
if (root == null) return null;
148+
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
149+
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
150+
return root;
151+
}
152+
}
153+
```
154+
107155
### **...**
108156

109157
```

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md

+35-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ class Solution:
7373
return root
7474
left = self.lowestCommonAncestor(root.left, p, q)
7575
right = self.lowestCommonAncestor(root.right, p, q)
76-
return right if left is None else (left if right is None else root)
76+
if left is None:
77+
return right
78+
if right is None:
79+
return left
80+
return root
7781
```
7882

7983
### **Java**
@@ -92,16 +96,42 @@ class Solution:
9296
*/
9397
class Solution {
9498
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
95-
if (root == null || root == p || root == q) {
96-
return root;
97-
}
99+
if (root == null || root == p || root == q) return root;
98100
TreeNode left = lowestCommonAncestor(root.left, p, q);
99101
TreeNode right = lowestCommonAncestor(root.right, p, q);
100-
return left == null ? right : (right == null ? left : root);
102+
if (left == null) return right;
103+
if (right == null) return left;
104+
return root;
101105
}
102106
}
103107
```
104108

109+
### **JavaScript**
110+
111+
```js
112+
/**
113+
* Definition for a binary tree node.
114+
* function TreeNode(val) {
115+
* this.val = val;
116+
* this.left = this.right = null;
117+
* }
118+
*/
119+
/**
120+
* @param {TreeNode} root
121+
* @param {TreeNode} p
122+
* @param {TreeNode} q
123+
* @return {TreeNode}
124+
*/
125+
var lowestCommonAncestor = function (root, p, q) {
126+
if (!root || root == p || root == q) return root;
127+
const left = lowestCommonAncestor(root.left, p, q);
128+
const right = lowestCommonAncestor(root.right, p, q);
129+
if (!left) return right;
130+
if (!right) return left;
131+
return root;
132+
};
133+
```
134+
105135
### **...**
106136

107137
```

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md

+35-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ class Solution:
6565
return root
6666
left = self.lowestCommonAncestor(root.left, p, q)
6767
right = self.lowestCommonAncestor(root.right, p, q)
68-
return right if left is None else (left if right is None else root)
68+
if left is None:
69+
return right
70+
if right is None:
71+
return left
72+
return root
6973
```
7074

7175
### **Java**
@@ -82,16 +86,42 @@ class Solution:
8286
*/
8387
class Solution {
8488
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
85-
if (root == null || root == p || root == q) {
86-
return root;
87-
}
89+
if (root == null || root == p || root == q) return root;
8890
TreeNode left = lowestCommonAncestor(root.left, p, q);
8991
TreeNode right = lowestCommonAncestor(root.right, p, q);
90-
return left == null ? right : (right == null ? left : root);
92+
if (left == null) return right;
93+
if (right == null) return left;
94+
return root;
9195
}
9296
}
9397
```
9498

99+
### **JavaScript**
100+
101+
```js
102+
/**
103+
* Definition for a binary tree node.
104+
* function TreeNode(val) {
105+
* this.val = val;
106+
* this.left = this.right = null;
107+
* }
108+
*/
109+
/**
110+
* @param {TreeNode} root
111+
* @param {TreeNode} p
112+
* @param {TreeNode} q
113+
* @return {TreeNode}
114+
*/
115+
var lowestCommonAncestor = function (root, p, q) {
116+
if (!root || root == p || root == q) return root;
117+
const left = lowestCommonAncestor(root.left, p, q);
118+
const right = lowestCommonAncestor(root.right, p, q);
119+
if (!left) return right;
120+
if (!right) return left;
121+
return root;
122+
};
123+
```
124+
95125
### **...**
96126

97127
```

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/Solution.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
class Solution {
1111
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12-
if (root == null || root == p || root == q) {
13-
return root;
14-
}
12+
if (root == null || root == p || root == q) return root;
1513
TreeNode left = lowestCommonAncestor(root.left, p, q);
1614
TreeNode right = lowestCommonAncestor(root.right, p, q);
17-
return left == null ? right : (right == null ? left : root);
15+
if (left == null) return right;
16+
if (right == null) return left;
17+
return root;
1818
}
1919
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function (root, p, q) {
15+
if (!root || root == p || root == q) return root;
16+
const left = lowestCommonAncestor(root.left, p, q);
17+
const right = lowestCommonAncestor(root.right, p, q);
18+
if (!left) return right;
19+
if (!right) return left;
20+
return root;
21+
};

0 commit comments

Comments
 (0)