Skip to content

Latest commit

 

History

History

0103.binary-tree-zigzag-level-order-traversal

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

题目描述

给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

例如:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回锯齿形层次遍历如下:

[
  [3],
  [20,9],
  [15,7]
]

解题思路

  1. BFS

具体解法

Golang

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func zigzagLevelOrder(root *TreeNode) [][]int {
	res := [][]int{}
	if root == nil {
		return res
	}
	queue := []*TreeNode{root}
	for level := 0; len(queue) > 0; level++ {
		l := len(queue)
		list := []int{}
		for i := 0; i < l; i++ {
			node := queue[i]
			if level%2 == 0 {
				list = append(list, node.Val)
			} else {
				list = append([]int{node.Val}, list...)
			}
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		queue = queue[l:]
		res = append(res, list)
	}
	return res
}