diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/1.png b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/1.png new file mode 100644 index 000000000..5d4c50ae4 Binary files /dev/null and b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/1.png differ diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/2.png b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/2.png new file mode 100644 index 000000000..56574fb43 Binary files /dev/null and b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/2.png differ diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/3.png b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/3.png new file mode 100644 index 000000000..e7a7aaf62 Binary files /dev/null and b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/3.png differ diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/README.md b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/README.md index 452ed5ecd..fac67ce16 100644 --- a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/README.md +++ b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution.go b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution.go index d115ccf5e..beb38ed9f 100644 --- a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution.go +++ b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution_test.go b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution_test.go index 14ff50eb4..7921dae51 100644 --- a/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution_test.go +++ b/leetcode/901-1000/0993.Cousins-in-Binary-Tree/Solution_test.go @@ -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() { }