Skip to content

Commit

Permalink
Merge pull request #651 from 0xff-dev/2196
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2196
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 8c32a87 + 2e67bd8 commit 9a4bb76
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [2196.Create Binary Tree From Descriptions][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
You are given a 2D integer array `descriptions` where `descriptions[i] = [parenti, childi, isLefti]` indicates that `parenti` is the **parent** of `childi` in a **binary** tree of **unique** values. Furthermore,

- If `isLefti == 1`, then `childi` is the left child of `parenti`.
- If `isLefti == 0`, then `childi` is the right child of `parenti`.

Construct the binary tree described by `descriptions` and return its **root**.

The test cases will be generated such that the binary tree is **valid**.

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

![example1](./example1drawio.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
Output: [50,20,80,15,17,19]
Explanation: The root node is the node with value 50 since it has no parent.
The resulting binary tree is shown in the diagram.
```

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

## 题解
![example2](./example2drawio.png)

### 思路1
> ...
Create Binary Tree From Descriptions
```go
```

Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]]
Output: [1,2,null,null,3,4]
Explanation: The root node is the node with value 1 since it has no parent.
The resulting binary tree is shown in the diagram.
```

## 结语

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

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

func Solution(descriptions [][]int) *TreeNode {
indies := [100001]*TreeNode{}
children := [100001]bool{}
for _, desc := range descriptions {
parent, child, left := desc[0], desc[1], desc[2]
children[child] = true
if indies[parent] == nil {
indies[parent] = &TreeNode{Val: parent}
}
if indies[child] == nil {
indies[child] = &TreeNode{Val: child}
}
if left == 1 {
indies[parent].Left = indies[child]
} else {
indies[parent].Right = indies[child]
}
}
for _, desc := range descriptions {
if !children[desc[0]] {
return indies[desc[0]]
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,35 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect *TreeNode
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{
{20, 15, 1}, {20, 17, 0}, {50, 20, 1}, {50, 80, 0}, {80, 19, 1},
}, &TreeNode{
Val: 50,
Left: &TreeNode{
Val: 20,
Left: &TreeNode{Val: 15},
Right: &TreeNode{Val: 17},
},
Right: &TreeNode{
Val: 80,
Left: &TreeNode{Val: 19},
},
}},
{"TestCase2", [][]int{
{1, 2, 1}, {2, 3, 0}, {3, 4, 1},
}, &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Right: &TreeNode{
Val: 3,
Left: &TreeNode{Val: 4},
},
},
}},
}

// 开始测试
Expand All @@ -30,10 +53,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
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.
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 9a4bb76

Please sign in to comment.