Skip to content

Commit

Permalink
Merge pull request #589 from 0xff-dev/865
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 865
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 79de389 + 5f7b0df commit 1521f51
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 @@
# [865.Smallest Subtree with all the Deepest Nodes][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, the depth of each node is **the shortest distance to the root**.

Return the smallest subtree such that it contains **all the deepest nodes** in the original tree.

A node is called **the deepest** if it has the largest depth possible among any node in the entire tree.

The **subtree** of a node is a tree consisting of that node, plus the set of all descendants of that node.

**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 nodes of the tree.
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
```

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

## 题解

### 思路1
> ...
Smallest Subtree with all the Deepest Nodes
```go
```
Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree.
```

**Example 3:**

```
Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
```

## 结语

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 1521f51

Please sign in to comment.