/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type MyCircularQueue struct {
Parr []*TreeNode
Head int
Tail int
Length int
Cap int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
Parr: make([]*TreeNode, k),
Head: 0,
Tail: 0,
Length: k,
Cap: 0,
}
}
func (this *MyCircularQueue) EnQueue(value *TreeNode) bool {
if this.Head == this.Tail && this.Cap > 0{
return false
}
this.Parr[this.Tail] = value
this.Tail = this.Tail + 1
if this.Tail == this.Length {
this.Tail = 0
}
this.Cap = this.Cap + 1
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.Head == this.Tail && this.Cap == 0 {
return false
}
this.Head = this.Head + 1
if this.Head == this.Length {
this.Head = 0
}
this.Cap = this.Cap - 1
return true
}
func (this *MyCircularQueue) Front() *TreeNode {
if this.IsEmpty() {
return nil
}
return this.Parr[this.Head]
}
func (this *MyCircularQueue) Rear() *TreeNode {
if this.IsEmpty() {
return nil
}
index := (this.Tail - 1 + this.Length) % this.Length
return this.Parr[index]
}
func (this *MyCircularQueue) IsEmpty() bool {
if this.Tail == this.Head && this.Cap == 0{
return true
}else {
return false
}
}
func (this *MyCircularQueue) IsFull() bool {
if this.Head == this.Tail && this.Cap > 0{
return true
}else {
return false
}
}
func levelOrder(root *TreeNode) [][]int {
cirQueue := Constructor(1000)
rsp := make([][]int, 0)
if nil == root {
return rsp
}
cirQueue.EnQueue(root)
for ; !cirQueue.IsEmpty(); {
size := cirQueue.Cap
tempSlict := make([]int, 0)
for i := 0; i < size; i = i + 1 {
node := cirQueue.Front()
tempSlict = append(tempSlict, node.Val)
cirQueue.DeQueue()
if node.Left != nil {
cirQueue.EnQueue(node.Left)
}
if node.Right != nil {
cirQueue.EnQueue(node.Right)
}
}
rsp = append(rsp, tempSlict)
}
return rsp
}