牛客题霸 NC14 按之字形顺序打印二叉树
生活随笔
收集整理的這篇文章主要介紹了
牛客题霸 NC14 按之字形顺序打印二叉树
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
解決方案
Go
func Print(pRoot *TreeNode) [][]int {NC14dfs(pRoot, 0)return NC14ans }var NC14ans = make([][]int, 0)func NC14dfs(pRoot *TreeNode, num int) {if pRoot == nil {return}if len(NC14ans) == num {NC14ans = append(NC14ans, []int{})}if num%2 == 1 {NC14ans[num] = append(NC14ans[num], pRoot.Val)} else {NC14ans[num] = append([]int{pRoot.Val}, NC14ans[num]...)}if pRoot.Right != nil {NC14dfs(pRoot.Right, num+1)}if pRoot.Left != nil {NC14dfs(pRoot.Left, num+1)} }參考文章
總結
以上是生活随笔為你收集整理的牛客题霸 NC14 按之字形顺序打印二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客题霸 NC13 二叉树的最大深度
- 下一篇: 牛客题霸 NC15 求二叉树的层序遍历