Merge pull request #181 from novahe/master

update/0515: add other solution
This commit is contained in:
halfrost
2021-11-06 18:28:03 -07:00
committed by GitHub
2 changed files with 44 additions and 0 deletions

View File

@ -92,3 +92,25 @@ func largestValues1(root *TreeNode) []int {
}
return res
}
// 解法三 深度遍历二叉树
func largestValues3(root *TreeNode) []int {
var res []int
var dfs func(root *TreeNode, level int)
dfs = func(root *TreeNode, level int) {
if root == nil {
return
}
if len(res) == level {
res = append(res, root.Val)
}
if res[level] < root.Val {
res[level] = root.Val
}
dfs(root.Right, level+1)
dfs(root.Left, level+1)
}
dfs(root, 0)
return res
}