二叉树遍历
前序遍历:先访问根节点,再前序遍历左子树,再前序遍历右子树 中序遍历:先中序遍历左子树,再访问根节点,再中序遍历右子树 后序遍历:先后序遍历左子树,再后序遍历右子树,再访问根节点
递归遍历
前中后序递归遍历换换位置即可
func preorderTraversal(root *TreeNode) {
if root == nil {
return
}
fmt.Println(root.Val)
preorderTraversal(root.Left)
preorderTraversal(root.Right)
}
前序非递归
func preorderTraversal(root *TreeNode) []int {
if root == nil{
return nil
}
result:=make([]int,0)
stack:=make([]*TreeNode,0)
for root!=nil || len(stack)!=0{
for root !=nil{
result=append(result,root.Val)
stack=append(stack,root)
root=root.Left
}
node:=stack[len(stack)-1]
stack=stack[:len(stack)-1]
root=node.Right
}
return result
}
中序非递归
func inorderTraversal(root *TreeNode) []int {
result := make([]int, 0)
if root == nil {
return result
}
stack := make([]*TreeNode, 0)
for len(stack) > 0 || root != nil {
for root != nil {
stack = append(stack, root)
root = root.Left
}
val := stack[len(stack)-1]
stack = stack[:len(stack)-1]
result = append(result, val.Val)
root = val.Right
}
return result
}
后续非递归
核心就是:根节点必须在右节点弹出之后,再弹出
func postorderTraversal(root *TreeNode) []int {
if root == nil {
return nil
}
result := make([]int, 0)
stack := make([]*TreeNode, 0)
var lastVisit *TreeNode
for root != nil || len(stack) != 0 {
for root != nil {
stack = append(stack, root)
root = root.Left
}
node:= stack[len(stack)-1]
if node.Right == nil || node.Right == lastVisit {
stack = stack[:len(stack)-1]
result = append(result, node.Val)
lastVisit = node
} else {
root = node.Right
}
}
return result
}
DFS 深搜-从上到下
func preorderTraversal(root *TreeNode) []int {
result := make([]int, 0)
dfs(root, &result)
return result
}
func dfs(root *TreeNode, result *[]int) {
if root == nil {
return
}
*result = append(*result, root.Val)
dfs(root.Left, result)
dfs(root.Right, result)
}
DFS 深搜-从下向上(分治法)
func preorderTraversal(root *TreeNode) []int {
result := divideAndConquer(root)
return result
}
func divideAndConquer(root *TreeNode) []int {
result := make([]int, 0)
if root == nil {
return result
}
left := divideAndConquer(root.Left)
right := divideAndConquer(root.Right)
result = append(result, root.Val)
result = append(result, left...)
result = append(result, right...)
return result
}
DFS 深度搜索(从上到下) 和分治法区别:前者一般将最终结果通过指针参数传入,后者一般递归返回结果最后合并
BFS 层次遍历
func levelOrder(root *TreeNode) [][]int {
result := make([][]int, 0)
if root == nil {
return result
}
queue := make([]*TreeNode, 0)
queue = append(queue, root)
for len(queue) > 0 {
list := make([]int, 0)
l := len(queue)
for i := 0; i < l; i++ {
level := queue[0]
queue = queue[1:]
list = append(list, level.Val)
if level.Left != nil {
queue = append(queue, level.Left)
}
if level.Right != nil {
queue = append(queue, level.Right)
}
}
result = append(result, list)
}
return result
}
|