funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}dfsPreorder(root,0,&res)return res
}funcdfsPreorder(root *TreeNode, depth int, res *[][]int){if root ==nil{return}iflen(*res)< depth+1{*res =append(*res,[]int{})}// Odd levelif depth &1==1{(*res)[depth]=prepend(root.Val,(*res)[depth])}else{(*res)[depth]=append((*res)[depth], root.Val)}dfsPreorder(root.Left, depth+1, res)dfsPreorder(root.Right, depth+1, res)}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}
funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}dfsInorder(root,0,&res)return res
}funcdfsInorder(root *TreeNode, depth int, res *[][]int){if root ==nil{return}iflen(*res)< depth+1{*res =append(*res,[]int{})}dfsInorder(root.Left, depth+1, res)// Odd levelif depth &1==1{(*res)[depth]=prepend(root.Val,(*res)[depth])}else{(*res)[depth]=append((*res)[depth], root.Val)}dfsInorder(root.Right, depth+1, res)}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}
funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}dfsPostorder(root,0,&res)return res
}funcdfsPostorder(root *TreeNode, depth int, res *[][]int){if root ==nil{return}iflen(*res)< depth+1{*res =append(*res,[]int{})}dfsPostorder(root.Left, depth+1, res)dfsPostorder(root.Right, depth+1, res)// Odd levelif depth &1==1{(*res)[depth]=prepend(root.Val,(*res)[depth])}else{(*res)[depth]=append((*res)[depth], root.Val)}}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}
funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}// Stack
st :=[]NodeWithDepth{}
cur := NodeWithDepth{Node: root, Depth:0}// Preorderfor cur.Node !=nil||len(st)>0{for cur.Node !=nil{iflen(res)< cur.Depth+1{
res =append(res,[]int{})}// odd levelif cur.Depth &1==1{
res[cur.Depth]=prepend(cur.Node.Val, res[cur.Depth])}else{
res[cur.Depth]=append(res[cur.Depth], cur.Node.Val)}
st =append(st, cur)
cur = NodeWithDepth{Node: cur.Node.Left, Depth: cur.Depth+1}}
cur, st = st[len(st)-1], st[:len(st)-1]
cur = NodeWithDepth{Node: cur.Node.Right, Depth: cur.Depth+1}}return res
}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}type NodeWithDepth struct{
Node *TreeNode
Depth int}
funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}// Stack
st :=[]NodeWithDepth{}
cur := NodeWithDepth{Node: root, Depth:0}// Preorderfor cur.Node !=nil||len(st)>0{for cur.Node !=nil{iflen(res)< cur.Depth+1{
res =append(res,[]int{})}
st =append(st, cur)
cur = NodeWithDepth{Node: cur.Node.Left, Depth: cur.Depth+1}}
cur, st = st[len(st)-1], st[:len(st)-1]// odd levelif cur.Depth &1==1{
res[cur.Depth]=prepend(cur.Node.Val, res[cur.Depth])}else{
res[cur.Depth]=append(res[cur.Depth], cur.Node.Val)}
cur = NodeWithDepth{Node: cur.Node.Right, Depth: cur.Depth+1}}return res
}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}type NodeWithDepth struct{
Node *TreeNode
Depth int}
funczigzagLevelOrder(root *TreeNode)[][]int{
res :=[][]int{}if root ==nil{return res
}// Stack
st :=[]NodeWithDepth{}
cur := NodeWithDepth{Node: root, Depth:0}
prev := NodeWithDepth{}// Preorderfor cur.Node !=nil||len(st)>0{for cur.Node !=nil{iflen(res)< cur.Depth+1{
res =append(res,[]int{})}
st =append(st, cur)
cur = NodeWithDepth{Node: cur.Node.Left, Depth: cur.Depth+1}}
cur = st[len(st)-1]if cur.Node.Right !=nil&& cur.Node.Right != prev.Node {
cur = NodeWithDepth{Node: cur.Node.Right, Depth: cur.Depth+1}}else{
st = st[:len(st)-1]// Odd level if cur.Depth &1==1{
res[cur.Depth]=prepend(cur.Node.Val, res[cur.Depth])}else{
res[cur.Depth]=append(res[cur.Depth], cur.Node.Val)}
prev = cur
cur = NodeWithDepth{}}}return res
}funcprepend(x int, a []int)[]int{
a =append(a,0)copy(a[1:], a)
a[0]= x
return a
}type NodeWithDepth struct{
Node *TreeNode
Depth int}