diff --git a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/README.md b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/README.md index 205d98382..288b79bf1 100755 --- a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/README.md +++ b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution.go b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution.go index d115ccf5e..8c1f3a114 100644 --- a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution.go +++ b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution.go @@ -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 } diff --git a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution_test.go b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution_test.go index 14ff50eb4..bb627b9b5 100644 --- a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution_test.go +++ b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/Solution_test.go @@ -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}, + }, + }, + }}, } // 开始测试 @@ -30,10 +53,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example1drawio.png b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example1drawio.png new file mode 100644 index 000000000..a8cc13acf Binary files /dev/null and b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example1drawio.png differ diff --git a/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example2drawio.png b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example2drawio.png new file mode 100644 index 000000000..76c5db89a Binary files /dev/null and b/leetcode/2101-2200/2196.Create-Binary-Tree-From-Descriptions/example2drawio.png differ