代码随想录打卡 Day14

前序遍历

递归法
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func preorderTraversal(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
 * }
 */
func preorderTraversal(root *TreeNode) []int {
    var res []int
    if 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
 * }
 */
func postorderTraversal(root *TreeNode) []int {
    var res []int
    var 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
 * }
 */
 //前序 中左右 -> 中右左
func postorderTraversal(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
}
func reverse(num []int){
    i , j := 0 , len(num) - 1
    for 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
 * }
 */
func inorderTraversal(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
 * }
 */
func inorderTraversal(root *TreeNode) []int {
    s := list.New()
    cur := root
    var res []int
    for 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
}

统一迭代法

前序遍历
func preorderTraversal(root *TreeNode) []int {
    var res []int
    if root == nil{
        return nil
    }
    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)
        }
        if(node.Left != nil){
            stack.PushBack(node.Left)
        }
        stack.PushBack(node)
        stack.PushBack(nil)
    }
    return res
}
后序遍历
func postorderTraversal(root *TreeNode) []int {
    var res []int
    if 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
 * }
 */
func inorderTraversal(root *TreeNode) []int {
    var res []int 
    if 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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值