第108题
func sortedArrayToBST(nums []int) *TreeNode {
if len(nums) <= 0 {
return nil
}
mid := len(nums) / 2
root := &TreeNode{nums[mid], nil, nil}
root.Left = sortedArrayToBST(nums[:mid])
root.Right = sortedArrayToBST(nums[mid+1:])
return root
}
第95题
func generateTrees(n int) (ans []*TreeNode) {
if n == 0 {
return nil
}
return helper(1, n)
}
func helper(l, r int) []*TreeNode {
if l > r {
return []*TreeNode{nil}
}
res := []*TreeNode{}
for i := l; i <= r; i++ {
leftTreeList := helper(l, i - 1)
rightTreeList := helper(i + 1, r)
for _, left := range leftTreeList {
for _, right := range rightTreeList {
root := &TreeNode{Val : i, Left : nil, Right : nil}
root.Left = left
root.Right = right
res = append(res, root)
}
}
}
return res
}
第144题
// 前序
func preorderTraversal(root *TreeNode) (ans []int) {
ptr := root
stack := []*TreeNode{}
for ptr != nil || len(stack) > 0 {
if ptr != nil {
ans = append(ans, ptr.Val)
stack = append(stack, ptr)
ptr = ptr.Left
} else {
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
ptr = top.Right
}
}
return
}
第94题
// 中序
func inorderTraversal(root *TreeNode) (ans []int) {
ptr := root
stack := []*TreeNode{}
for ptr != nil || len(stack) > 0 {
if ptr != nil {
stack = append(stack, ptr)
ptr = ptr.Left
} else {
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
ans = append(ans, top.Val)
ptr = top.Right
}
}
return
}
第145题
// 后序
func postorderTraversal(root *TreeNode) (ans []int) {
ptr := root
stack := []*TreeNode{}
var pre *TreeNode
for ptr != nil || len(stack) > 0 {
if ptr != nil {
stack = append(stack, ptr)
ptr = ptr.Left
} else {
top := stack[len(stack)-1]
if top.Right != nil && top.Right != pre {
ptr = top.Right
} else {
ans = append(ans, top.Val)
stack = stack[:len(stack)-1]
pre = top
}
}
}
return
}
第102题
func levelOrder(root *TreeNode) (ans [][]int) {
if root == nil {
return nil
}
queue := make([]*TreeNode, 0)
queue = append(queue, root)
n := 1
for len(queue) > 0 {
comb := make([]int, 0)
for i := 0; i < n; i++ {
front := queue[0]
queue = queue[1:]
if front.Left != nil {
queue = append(queue, front.Left)
}
if front.Right != nil {
queue = append(queue, front.Right)
}
comb = append(comb, front.Val)
}
ans = append(ans, comb)
n = len(queue)
}
return
}
第103题
func zigzagLevelOrder(root *TreeNode) (ans [][]int) {
if root == nil {
return nil
}
queue := make([]*TreeNode, 0)
queue = append(queue, root)
n := 1
level := 1
for len(queue) > 0 {
comb := make([]int, 0)
for i := 0; i < n; i++ {
front := queue[0]
queue = queue[1:]
if front.Left != nil {
queue = append(queue, front.Left)
}
if front.Right != nil {
queue = append(queue, front.Right)
}
comb = append(comb, front.Val)
}
if level % 2 == 0 {
for i, n := 0, len(comb); i < n/2; i++ {
comb[i], comb[n-1-i] = comb[n-1-i], comb[i]
}
}
ans = append(ans, comb)
n = len(queue)
level++
}
return
}
第429题
func levelOrder(root *Node) (ans [][]int) {
if root == nil {
return nil
}
queue := make([]*Node, 0)
queue = append(queue, root)
curLen := 1
for len(queue) > 0 {
comb := make([]int, 0)
for i := 0; i < curLen; i++ {
front := queue[0]
queue = queue[1:]
comb = append(comb, front.Val)
for _, child := range front.Children {
if child != nil {
queue = append(queue, child)
}
}
}
ans = append(ans, append([]int(nil), comb...))
curLen = len(queue)
}
return
}