Skip to content

Commit

Permalink
Merge pull request #588 from 0xff-dev/951
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 951
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 4a50c3a + 059fa76 commit c82bdfe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 25 deletions.
35 changes: 21 additions & 14 deletions leetcode/901-1000/0951.Flip-Equivalent-Binary-Trees/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [951.Flip Equivalent Binary Trees][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
For a binary tree **T**, we can define a **flip operation** as follows: choose any node, and swap the left and right child subtrees.

A binary tree **X** is flip equivalent to a binary tree **Y** if and only if we can make **X** equal to **Y** after some number of flip operations.

Given the roots of two binary trees `root1` and `root2`, return `true` if the two trees are flip equivalent or `false` otherwise.

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

![example1](./tree_ex.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.
```

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

### 思路1
> ...
Flip Equivalent Binary Trees
```go
```
Input: root1 = [], root2 = []
Output: true
```

**Example 3:**

```
Input: root1 = [], root2 = [1]
Output: false
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/901-1000/0951.Flip-Equivalent-Binary-Trees/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

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

func Solution(root1 *TreeNode, root2 *TreeNode) bool {
if root1 == nil && root2 == nil {
return true
}
if root1 == nil || root2 == nil {
return false
}

if root1.Val != root2.Val {
return false
}
a := Solution(root1.Left, root2.Left)
b := Solution(root1.Right, root2.Right)

if a && b {
return true
}
a = Solution(root1.Left, root2.Right)
b = Solution(root1.Right, root2.Left)
if a && b {
return true
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,62 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
t1, t2 *TreeNode
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: 5,
Left: &TreeNode{Val: 7},
Right: &TreeNode{Val: 8},
},
},
Right: &TreeNode{
Val: 3,
Left: &TreeNode{Val: 6},
},
}, &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 3,
Right: &TreeNode{Val: 6},
},
Right: &TreeNode{
Val: 2,
Left: &TreeNode{
Val: 4,
},
Right: &TreeNode{
Val: 5,
Left: &TreeNode{Val: 8},
Right: &TreeNode{Val: 7},
},
},
}, true},
{"TestCase1", nil, nil, true},
{"TestCase2", nil, &TreeNode{Val: 1}, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.t1, c.t2)
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",
c.expect, got, c.t1, c.t2)
}
})
}
}

// 压力测试
// 压力测试
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 c82bdfe

Please sign in to comment.