Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 993 #945

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 23 additions & 13 deletions leetcode/901-1000/0993.Cousins-in-Binary-Tree/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [993.Cousins in Binary Tree][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 with unique values and the values of two different nodes of the tree `x` and `y`, return `true` if the nodes corresponding to the values `x` and `y` in the tree are **cousins**, or `false` otherwise.

Two nodes of a binary tree are **cousins** if they have the same depth with different parents.

Note that in a binary tree, the root node is at the depth `0`, and children of each depth `k` node are at the depth `k + 1`.

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

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [1,2,3,4], x = 4, y = 3
Output: false
```

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

## 题解
![2](./2.png)

### 思路1
> ...
Cousins in Binary Tree
```go
```
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true
```

**Example 3:**

![3](./3.png)

```
Input: root = [1,2,3,null,4], x = 2, y = 3
Output: false
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

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

func Solution(root *TreeNode, x int, y int) bool {
var (
xnode *TreeNode
ynode *TreeNode
xd, yd int
)
var dfs func(*TreeNode, *TreeNode, int)
dfs = func(fa, tree *TreeNode, dep int) {
if tree == nil {
return
}
if tree.Val == x {
xnode = fa
xd = dep
}
if tree.Val == y {
ynode = fa
yd = dep
}
dfs(tree, tree.Left, dep+1)
dfs(tree, tree.Right, dep+1)
}
dfs(nil, root, 0)
if xd != yd {
return false
}
return xnode != ynode
}
45 changes: 36 additions & 9 deletions leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,57 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
tree *TreeNode
x, y int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Left: &TreeNode{Val: 4},
},
Right: &TreeNode{Val: 3},
}, 4, 3, false},
{"TestCase2", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Right: &TreeNode{Val: 4},
},
Right: &TreeNode{
Val: 3,
Right: &TreeNode{Val: 5},
},
}, 5, 4, true},
{"TestCase3", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Right: &TreeNode{
Val: 4,
},
},
Right: &TreeNode{Val: 3},
}, 2, 3, false},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading