Skip to content

Commit

Permalink
Merge pull request #590 from 0xff-dev/1123
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1123
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 3fc788e + 8aa35d5 commit 79de389
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [1123.Lowest Common Ancestor of Deepest Leaves][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given the `root` of a binary tree, return the lowest common ancestor of its deepest leaves.

Recall that:

- The node of a binary tree is a leaf if and only if it has no children
- The depth of the root of the tree is `0`. if the depth of a node is `d`, the depth of each of its children is `d + 1`.
- The lowest common ancestor of a set `S` of nodes, is the node `A` with the largest depth such that every node in `S` is in the subtree with root `A`.

**Example 1:**
**Example 1:**

![example1](./sketch1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest leaf-nodes of the tree.
Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Lowest Common Ancestor of Deepest Leaves
```go
```
Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree, and it's the lca of itself.
```

**Example 3:**

```
Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
package Solution

func Solution(x bool) bool {
return x
type TreeNode struct {
Val int
Left, Right *TreeNode
}

func Solution(root *TreeNode) *TreeNode {
var dfs func(*TreeNode, int)
maxDep := 0
targetLeafNodes := 0
nodeDep := make(map[int]int)
dfs = func(now *TreeNode, dep int) {
if now == nil {
return
}
nodeDep[now.Val] = dep
if now.Left == nil && now.Right == nil {
if dep > maxDep {
maxDep = dep
targetLeafNodes = 1
} else if dep == maxDep {
targetLeafNodes++
}
return
}
dfs(now.Left, dep+1)
dfs(now.Right, dep+1)
}
dfs(root, 1)

var ans *TreeNode
var dfs1 func(*TreeNode) int
dfs1 = func(now *TreeNode) int {
if now == nil {
return 0
}
left := dfs1(now.Left)
right := dfs1(now.Right)

addOne := 0
if nodeDep[now.Val] == maxDep {
addOne = 1
}
r := left + right + addOne
if r == targetLeafNodes {
if ans == nil || nodeDep[ans.Val] < nodeDep[now.Val] {
ans = now
}
}
return r
}
dfs1(root)

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,55 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *TreeNode
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 3,
Left: &TreeNode{
Val: 5,
Left: &TreeNode{
Val: 6,
},
Right: &TreeNode{
Val: 2,
Left: &TreeNode{Val: 7},
Right: &TreeNode{Val: 4},
},
},
Right: &TreeNode{
Val: 1,
Left: &TreeNode{Val: 0},
Right: &TreeNode{Val: 8},
},
}, 2},
{"TestCase2", &TreeNode{Val: 1}, 1},
{"TestCase3", &TreeNode{
Val: 0,
Left: &TreeNode{
Val: 1,
Right: &TreeNode{Val: 2},
},
Right: &TreeNode{Val: 3},
}, 2},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
if !reflect.DeepEqual(got, c.expect) {
if !reflect.DeepEqual(got.Val, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 79de389

Please sign in to comment.