Skip to content

Commit cf81100

Browse files
committed
Add 235 236
1 parent 171699d commit cf81100

File tree

6 files changed

+279
-0
lines changed

6 files changed

+279
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [236. Lowest Common Ancestor of a Binary Tree][title]
2+
3+
## Description
4+
5+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
6+
7+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
8+
9+
Given binary search tree: `root = [6,2,8,0,4,7,9,null,null,3,5]`
10+
11+
```
12+
_______6______
13+
/ \
14+
___2__ ___8__
15+
/ \ / \
16+
0 _4 7 9
17+
/ \
18+
3 5
19+
```
20+
21+
**Example 1:**
22+
23+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
24+
25+
Output: 6
26+
27+
Explanation: The LCA of nodes 2 and 8 is 6.
28+
29+
**Example 2:**
30+
31+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
32+
33+
Output: 2
34+
35+
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself
36+
according to the LCA definition.
37+
38+
## NOTES
39+
40+
- All of the nodes' values will be unique.
41+
- `p` and `q` are different and both values will exist in the BST.
42+
43+
**Tags:** Binary Search Tree (BST)
44+
45+
## 题意
46+
> 在二叉搜索树中查询两个节点最近的祖先元素
47+
48+
### 思路1
49+
定义节点,直接左右递归, 详见代码。
50+
51+
代码实现可以查考`0236`实现, 但是考虑到`BST`的有序性, 判断逻辑可以更加精简。
52+
```go
53+
func Lowest(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
54+
if p.val < root.val && q.val < root.val {
55+
return Lowest(root.left, p, q)
56+
}
57+
if p.val > root.val && q.val > root.val {
58+
return Lowest(root.right, p, q)
59+
}
60+
return root
61+
}
62+
```
63+
64+
## 结语
65+
66+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-letcode][me]
67+
68+
[title]: https://leetcode.com/problems/valid-anagram/description/
69+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
val int
5+
left *TreeNode
6+
right *TreeNode
7+
}
8+
9+
func Lowest(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
10+
if p.val < root.val && q.val < root.val {
11+
return Lowest(root.left, p, q)
12+
}
13+
if p.val > root.val && q.val > root.val {
14+
return Lowest(root.right, p, q)
15+
}
16+
return root
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package Solution
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// 测试样例 root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
8+
func TestSolution(t *testing.T) {
9+
tNode := &TreeNode{}
10+
tNode.val = 6
11+
12+
tNode1 := &TreeNode{}
13+
tNode1.val = 2
14+
15+
tNode2 := &TreeNode{}
16+
tNode2.val = 8
17+
18+
tNode3 := &TreeNode{}
19+
tNode3.val = 0
20+
21+
tNode4 := &TreeNode{}
22+
tNode4.val = 4
23+
24+
tNode5 := &TreeNode{}
25+
tNode5.val = 7
26+
27+
tNode6 := &TreeNode{}
28+
tNode6.val = 9
29+
30+
tNode9 := &TreeNode{}
31+
tNode9.val = 3
32+
33+
tNode10 := &TreeNode{}
34+
tNode10.val = 5
35+
36+
// 第一层
37+
tNode.left = tNode1
38+
tNode.right = tNode2
39+
40+
// 第二层
41+
tNode1.left = tNode3
42+
tNode1.right = tNode4
43+
tNode2.left = tNode5
44+
tNode2.right = tNode6
45+
46+
// 第三层
47+
tNode4.left = tNode9
48+
tNode4.right = tNode10
49+
50+
luckyNode := Lowest(tNode, tNode1, tNode2)
51+
t.Logf("luckyNode.value = %d", luckyNode.val)
52+
if luckyNode != tNode {
53+
t.Fatalf("Unlucky, false.")
54+
}
55+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [236. Lowest Common Ancestor of a Binary Tree][title]
2+
3+
## Description
4+
5+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
6+
7+
According to the definition of [LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
8+
9+
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
10+
11+
```
12+
_______3______
13+
/ \
14+
___5__ ___1__
15+
/ \ / \
16+
6 _2 0 8
17+
/ \
18+
7 4
19+
```
20+
21+
**Example 1:**
22+
23+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
24+
25+
Output: 3
26+
27+
Explanation: The LCA of of nodes 5 and 1 is 3.
28+
29+
**Example 2:**
30+
31+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
32+
33+
Output: 5
34+
35+
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself
36+
according to the LCA definition.
37+
38+
## NOTES
39+
- All of the nodes' values will be unique.
40+
- `p` and `q` are different and both values will exist in the binary tree.
41+
42+
**Tags:** Binary Tree
43+
44+
## 题意
45+
> 查找二叉树某两个节点最近的祖先元素
46+
47+
### 思路1
48+
定义节点,直接左右递归, 详见代码
49+
```go
50+
type TreeNode struct {
51+
val int
52+
left *TreeNode
53+
right *TreeNode
54+
}
55+
```
56+
57+
## 结语
58+
59+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-letcode][me]
60+
61+
[title]: https://leetcode.com/problems/valid-anagram/description/
62+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
val int
5+
left *TreeNode
6+
right *TreeNode
7+
}
8+
9+
func Lowest(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
10+
if root == nil || p == root || q == root {
11+
return root
12+
}
13+
left := Lowest(root.left, p, q)
14+
right := Lowest(root.right, p, q)
15+
if left == nil {
16+
return right
17+
}
18+
if right == nil {
19+
return left
20+
}
21+
return root
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Solution
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// 测试样例 root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
8+
func TestSolution(t *testing.T) {
9+
tNode := &TreeNode{}
10+
tNode.val = 3
11+
12+
tNode1 := &TreeNode{}
13+
tNode1.val = 5
14+
15+
tNode2 := &TreeNode{}
16+
tNode2.val = 1
17+
18+
tNode3 := &TreeNode{}
19+
tNode3.val = 6
20+
21+
tNode4 := &TreeNode{}
22+
tNode4.val = 2
23+
24+
tNode5 := &TreeNode{}
25+
tNode5.val = 0
26+
27+
tNode6 := &TreeNode{}
28+
tNode6.val = 8
29+
30+
tNode9 := &TreeNode{}
31+
tNode9.val = 7
32+
33+
tNode10 := &TreeNode{}
34+
tNode10.val = 4
35+
36+
// 第一层
37+
tNode.left = tNode1
38+
tNode.right = tNode2
39+
40+
// 第二层
41+
tNode1.left = tNode3
42+
tNode1.right = tNode4
43+
tNode2.left = tNode5
44+
tNode2.right = tNode6
45+
46+
// 第三层
47+
tNode4.left = tNode9
48+
tNode4.right = tNode10
49+
50+
luckyNode := Lowest(tNode, tNode1, tNode2)
51+
if luckyNode != tNode {
52+
t.Fatalf("Unlucky, false.")
53+
}
54+
}

0 commit comments

Comments
 (0)