diff --git a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/README.md b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/README.md index 922bbc215..bd1ed2c83 100644 --- a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/README.md +++ b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/README.md @@ -4,14 +4,24 @@ > 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, the level of its root is 1, the level of its children is 2, and so on. + +Return the smallest level X such that the sum of all the values of nodes at level X is maximal. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: root = [1,7,0,7,-8,null,null] +Output: 2 +Explanation: +Level 1 sum = 1. +Level 2 sum = 7 + 0 = 7. +Level 3 sum = 7 + -8 = -1. +So we return the level with the maximum sum which is level 2. ``` +Link to question: https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/ + ## 题意 > ... diff --git a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution.go b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution.go index d115ccf5e..1c572c5c3 100644 --- a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution.go +++ b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution.go @@ -1,5 +1,53 @@ package Solution -func Solution(x bool) bool { - return x +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +func maxLevelSum(root *TreeNode) int { + tree := make(map[*TreeNode]int) + tree[root] = 0 + var treeList []*TreeNode + treeList = append(treeList, root) + sum := root.Val + l := 0 + var sumList []int + level := tree[root] + for len(treeList) > 0 { + r := treeList[0] + treeList = treeList[1:] + + if tree[r] > level { + sumList = append(sumList, sum) + sum = 0 + level = tree[r] + } + sum += r.Val + + if r.Left != nil { + treeList = append(treeList, r.Left) + tree[r.Left] = level + 1 + } + + if r.Right != nil { + treeList = append(treeList, r.Right) + tree[r.Right] = level + 1 + } + + } + + sumList = append(sumList, sum) + max := sumList[0] + for i, j := range sumList { + if j > max { + max = j + l = i + } + } + return l + 1 } diff --git a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go index 3e51ecacf..948828791 100644 --- a/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go +++ b/leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go @@ -2,40 +2,79 @@ package Solution import ( "reflect" - "strconv" + "runtime" "testing" + + "github.com/stretchr/testify/assert" ) -func TestSolution(t *testing.T) { - // 测试用例 - cases := []struct { - name string - inputs bool - expect bool - }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, - } +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} - // 开始测试 - for i, c := range cases { - t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) - if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) - } - }) - } +type SolutionFuncType func(*TreeNode) int + +var SolutionFuncList = []SolutionFuncType{ + maxLevelSum, } -// 压力测试 -func BenchmarkSolution(b *testing.B) { +var DefaultValue int = -1024 +func InsertNodeToTree(tree *TreeNode, node *TreeNode) { + if tree == nil { + return + } + if tree.Val == DefaultValue { + tree.Val = node.Val + return + } + if node.Val > tree.Val { + if tree.Right == nil { + tree.Right = &TreeNode{Val: DefaultValue} + } + InsertNodeToTree(tree.Right, node) + } + if node.Val < tree.Val { + if tree.Left == nil { + tree.Left = &TreeNode{Val: DefaultValue} + } + InsertNodeToTree(tree.Left, node) + } +} + +func InitTree(values ...int) (root *TreeNode) { + rootNode := TreeNode{Val: DefaultValue, Right: nil, Left: nil} + for _, value := range values { + node := TreeNode{Val: value} + InsertNodeToTree(&rootNode, &node) + } + return &rootNode } -// 使用案列 -func ExampleSolution() { +func TestSolution(t *testing.T) { + // 测试用例 + ast := assert.New(t) + // The original Problem requires the binary tree constructed from array. Please refer to test cases produced at Leetcode problem https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/ + treeNode := InitTree(1, 7, 0, 7, -8) + + var cases = []struct { + name string + inputs *TreeNode + expect int + }{ + {"TestCase1", treeNode, 2}, + } + for _, f := range SolutionFuncList { + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + actual := f(c.inputs) + ast.Equal(c.expect, actual, + "func: %v case: %v ", + runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name) + }) + } + } } diff --git a/test.sh b/test.sh old mode 100644 new mode 100755