二叉树的层序遍历:就是从根节点root开始,从上到下,从左到右,依次遍历到每个子节点。
层序遍历的这类题目,一般都是要求返回特定的每层节点数组,其中每层可能是所有节点树、最大的节点数、最小的节点数、所有节点数的平均数、正序输出显示、逆序输出显示、每层第一个节点数、每层最后一个节点数等等
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
ret_arr := make([][]int, 0)
// 缓存队列
queue := []*TreeNode{root}
for len(queue) != 0 {
length := len(queue)
arr := make([]int, 0)
for i:=0;i<length;i++ {
// 取出队列中所有树节点的值(同一层的)
arr = append(arr, queue[i].Val)
// 将对应节点如果有左右子节点就加入到队列中
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
// 从队列中删除已取过值的树节点
queue = queue[length:]
ret_arr = append(ret_arr, arr)
}
return ret_arr
}
题目链接:LeetCode-107. 二叉树的层序遍历 II
func reverseArr(arr [][]int, left int, right int) {
for left<= right {
arr[left], arr[right] = arr[right], arr[left]
left++
right--
}
}
func levelOrderBottom(root *TreeNode) [][]int {
if root == nil {
return nil
}
ret_arr := make([][]int, 0)
queue := []*TreeNode{root}
for len(queue) != 0 {
arr := make([]int, 0)
length := len(queue)
for i:=0; i<length; i++ {
arr = append(arr, queue[i].Val)
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
ret_arr = append(ret_arr, arr)
}
reverseArr(ret_arr, 0, len(ret_arr)-1)
return ret_arr
}
func rightSideView(root *TreeNode) []int {
if root == nil {
return nil
}
ret_arr := make([]int, 0)
queue := []*TreeNode{root}
for len(queue) != 0 {
length := len(queue)
ret_arr = append(ret_arr, queue[length-1].Val)
for i:=0;i<length;i++ {
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
}
return ret_arr
}
func averageOfLevels(root *TreeNode) []float64 {
if root == nil {
return nil
}
ret_arr := make([]float64, 0)
queue := []*TreeNode{root}
for len(queue) != 0 {
sum := 0
length := len(queue)
for i:=0; i<length; i++ {
sum += queue[i].Val
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
ret_arr = append(ret_arr, float64(sum)/float64(length))
}
return ret_arr
}
func levelOrder(root *Node) [][]int {
if root == nil {
return nil
}
ret_arr := make([][]int, 0)
queue := []*Node{root}
for len(queue)!= 0 {
arr := make([]int, 0)
length := len(queue)
for i:=0; i<length; i++ {
arr = append(arr, queue[i].Val)
for j:=0; j<len(queue[i].Children); j++ {
queue = append(queue, queue[i].Children[j])
}
}
queue = queue[length:]
ret_arr = append(ret_arr, arr)
}
return ret_arr
}
func largestValues(root *TreeNode) []int {
if root == nil {
return nil
}
ret_arr := make([]int, 0)
queue := []*TreeNode{root}
for len(queue) != 0 {
max := queue[0].Val
length := len(queue)
for i:=0;i<length;i++{
if max < queue[i].Val {
max = queue[i].Val
}
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
ret_arr = append(ret_arr, max)
}
return ret_arr
}
题目链接:LeetCode-116. 填充每个节点的下一个右侧节点指针
func connect(root *Node) *Node {
if root == nil {
return root
}
queue := []*Node{root}
for len(queue) != 0 {
length := len(queue)
for i:=0;i<length;i++ {
if i!= length-1 {
queue[i].Next = queue[i+1]
}
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
}
return root
}
题目链接:LeetCode-103. 二叉树的锯齿形层序遍历
func reverseArr(arr []int, left int, right int) {
for left<=right {
arr[left], arr[right] = arr[right], arr[left]
left++
right--
}
}
func zigzagLevelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
ret_arr := make([][]int, 0)
queue := []*TreeNode{root}
odd := true
for len(queue) != 0 {
length := len(queue)
arr := make([]int, 0)
for i:=0;i<length;i++ {
arr = append(arr, queue[i].Val)
if queue[i].Left != nil {
queue = append(queue, queue[i].Left)
}
if queue[i].Right != nil {
queue = append(queue, queue[i].Right)
}
}
queue = queue[length:]
if !odd {
reverseArr(arr, 0, len(arr)-1)
}
odd = !odd
ret_arr = append(ret_arr, arr)
}
return ret_arr
}