/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcpreorderTraversal(root *TreeNode)[]int{var tranversal func(node *TreeNode)var res []int
tranversal =func(node *TreeNode){if(node ==nil){return}
res =append(res , node.Val)tranversal(node.Left)tranversal(node.Right)}tranversal(root)return res
}
迭代法
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcpreorderTraversal(root *TreeNode)[]int{var res []intif root ==nil{return res
}
s := list.New()
s.PushBack(root)for s.Len()>0{
node := s.Remove(s.Back()).(*TreeNode)
res =append(res,node.Val)if(node.Right !=nil){
s.PushBack(node.Right)}if(node.Left !=nil){
s.PushBack(node.Left)}}return res
}
后序遍历
递归法
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcpostorderTraversal(root *TreeNode)[]int{var res []intvar trans func(node *TreeNode)
trans =func(node *TreeNode){if(node ==nil){return}trans(node.Left)trans(node.Right)
res =append(res,node.Val)}trans(root)return res
}
迭代法
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*///前序 中左右 -> 中右左funcpostorderTraversal(root *TreeNode)[]int{if root ==nil{return[]int{}}
s := list.New()var res []int
s.PushBack(root)for s.Len()>0{
node := s.Remove(s.Back()).(*TreeNode)
res =append(res,node.Val)if(node.Left !=nil){
s.PushBack(node.Left)}if(node.Right !=nil){
s.PushBack(node.Right)}}reverse(res)return res
}funcreverse(num []int){
i , j :=0,len(num)-1for j > i{
num[i],num[j]= num[j],num[i]
i++
j--}}
中序遍历
递归法
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcinorderTraversal(root *TreeNode)[]int{var traversal func(node *TreeNode)
res :=make([]int,0)
traversal =func(node *TreeNode){if(node ==nil){return}traversal(node.Left)
res =append(res,node.Val)traversal(node.Right)}traversal(root)return res
}
迭代法
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcinorderTraversal(root *TreeNode)[]int{
s := list.New()
cur := root
var res []intfor s.Len()>0|| cur !=nil{if(cur !=nil){
s.PushBack(cur)
cur = cur.Left
}else{
cur = s.Remove(s.Back()).(*TreeNode)
res =append(res,cur.Val)
cur = cur.Right
}}return res
}
funcpostorderTraversal(root *TreeNode)[]int{var res []intif root ==nil{return res
}
s := list.New()
s.PushBack(root)for s.Len()>0{
n1 := s.Back()
s.Remove(n1)if n1.Value ==nil{
n2 := s.Remove(s.Back()).(*TreeNode)
res =append(res , n2.Val)continue}
node := n1.Value.(*TreeNode)
s.PushBack(node)
s.PushBack(nil)if(node.Right !=nil){
s.PushBack(node.Right)}if(node.Left !=nil){
s.PushBack(node.Left)}}return res
}
中序遍历
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcinorderTraversal(root *TreeNode)[]int{var res []intif root ==nil{return res
}
stack := list.New()
stack.PushBack(root)for stack.Len()>0{
n1 := stack.Back()
stack.Remove(n1)if n1.Value ==nil{
n2 := stack.Remove(stack.Back()).(*TreeNode)
res =append(res,n2.Val)continue}
node := n1.Value.(*TreeNode)if(node.Right !=nil){
stack.PushBack(node.Right)}
stack.PushBack(node)
stack.PushBack(nil)if(node.Left !=nil){
stack.PushBack(node.Left)}}return res
}