golang力扣刷题篇--二叉树

总结最近刷的关于二叉树的算法题:
94二叉树的中序遍历

func inorderTraversal(root *TreeNode) []int {
    if root==nil{
        return nil
    }
    var arrays []int
    arrays = append(arrays,inorderTraversal(root.Left)...)
    arrays = append(arrays,root.Val)
    arrays = append(arrays,inorderTraversal(root.Right)...)
    return arrays
}

100相同的树

func isSameTree(p *TreeNode, q *TreeNode) bool {
     if p==nil&&q==nil{
         return true
     }
     if p==nil||q==nil{
         return false
     }
     if p.Val!=q.Val{
         return false
     }
    return  isSameTree(p.Left,q.Left)&&isSameTree(p.Right,q.Right)

}

101对称二叉树

func isSymmetric(root *TreeNode) bool {
    if root ==nil{
        return true
    }
    return check(root,root)
}
func check(p,q *TreeNode) bool{
    if p==nil&&q==nil{
         return true
     }
     if p==nil||q==nil{
         return false 
     }
     return p.Val==q.Val&&check(p.Left,q.Right)&&check(p.Right,q.Left)
     }

102二叉树的层序遍历

 */
func levelOrder(root *TreeNode) [][]int {
      var queue []*TreeNode
       result :=[][]int{}
      levelres :=[]int{}
      if root ==nil{
          return [][]int{}
      }
      queue=append(queue,root)
    for len(queue)!=0{
         levelres=[]int{}
        for i:=0;i<len(queue);i++{
            levelres=append(levelres,queue[i].Val)
            if queue[i].Left!=nil{
                 queue =append(queue,queue[i].Left)
                // node=node.Left
            }
            if queue[i].Right!=nil{
                queue =append(queue,queue[i].Right)
                //node=node.Right
            }
        }
         queue=queue[len(queue):]
        result = append(result,levelres)
    }
    return result
}

104二叉树的最大深度

func maxDepth(root *TreeNode) int {
    if root ==nil{
        return 0
    }
    return max(maxDepth(root.Left),maxDepth(root.Right))+1
}  
 func max(a,b int) int{
        if a>b{
            return a
        }
        return b
}

110平衡二叉树

func isBalanced(root *TreeNode) bool {
      if root ==nil{
          return true
      }    
  return abs(getheight(root.Left) - getheight(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)

}
func getheight(root *TreeNode) int{
    if root ==nil{
        return 0
    }
    return max(getheight(root.Left),getheight(root.Right))+1
}
func max(a,b int) int{
    if a>b{
        return a
    }
    return b
}
func abs(a int) int{
    if a>=0{
        return a
    }
    return -a
}

111二叉树的最小深度

func minDepth(root *TreeNode) int {
   if root==nil{
       return 0
   }
   dl :=minDepth(root.Left)
   dr :=minDepth(root.Right)
   if root.Left==nil{
       return dr+1
   }else if root.Right==nil{
       return dl +1
   }else{return min(dl,dr)+1}
       return 0
}


func min (a,b int) int{
    if a<b{
        return a
    }
    return b
}

112路径总和

func hasPathSum(root *TreeNode, targetSum int) bool {
       if root == nil {
        return false
    }
      if root.Left==nil&&root.Right==nil{
        return  targetSum==root.Val
      }
      targetSum=targetSum-root.Val
      return hasPathSum(root.Left,targetSum)||hasPathSum(root.Right,targetSum)      
}

144前序遍历

func preorderTraversal(root *TreeNode) []int {
     var arrays []int
   /*  arrays = append(arrays,root.Val)
     arrays=append(arrays,preorderTraversal(root.Left)...)  
     arrays=append(arrays,preorderTraversal(root.Right)...) 
     return  arrays*/
     var stack []*TreeNode
     node :=root
     for node !=nil||len(stack)>0{
        for node!=nil{ 
            arrays = append(arrays,node.Val)
            stack = append(stack,node)
            node = node.Left
        }
    
        node =stack[len(stack)-1].Right
        stack=stack[:len(stack)-1]
     }
     return arrays
     
}

145后序遍历

func postorderTraversal(root *TreeNode) []int {
   if root==nil{
     return nil    
     }  
     var arrays []int
     arrays =append(arrays,postorderTraversal(root.Left)...)
     arrays =append(arrays,postorderTraversal(root.Right)...)
     arrays = append(arrays,root.Val)
     return arrays
}

235二叉搜索树的最近公共祖先

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    for root!=nil{
        val := root.Val
        switch {
            case p.Val<val&&q.Val<val:
                 root =root.Left
            case p.Val>val&&q.Val>val:
                root =root.Right
            default :
                 return root 
        }
    } 
    return nil
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值