代码随想录打卡 Day16

本文提供了几种树结构(二叉树、n叉树)的最大深度、最小深度以及完全二叉树节点个数的计算方法。通过递归和广度优先搜索(BFS)策略,实现了对树的深度遍历并计算相关属性。
摘要由CSDN通过智能技术生成

104.二叉树的最大深度

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    var seekDepth func(root *TreeNode,depth int)
    if root == nil{
        return 0
    }
    res := 0
    //queue := list.New()
    seekDepth = func(root *TreeNode,depth int){
        if root == nil{
            return 
        }
        if(res < depth){
            res = depth
        }
        seekDepth(root.Left,depth+1)
        seekDepth(root.Right,depth+1)
    }
    seekDepth(root,0)
    return res + 1
}

559.n叉树的最大深度

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Children []*Node
 * }
 */

func maxDepth(root *Node) int {
    count := 0
    if root == nil {
        return count
    }
    ss := list.New()
    ss.PushBack(root)
    for ss.Len() > 0{
        length := ss.Len()
        count++
        for i := 0 ; i < length ; i++{
            node := ss.Remove(ss.Front()).(*Node)
            for j := 0 ; j < len(node.Children) ; j++{
                ss.PushBack(node.Children[j])
            }
        }
    }
    return count
}

● 111.二叉树的最小深度

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minDepth(root *TreeNode) int {
    queue := list.New()
    if root == nil{
        return 0
    }
    queue.PushBack(root)
    depth := 0
    for queue.Len() > 0{
        length := queue.Len()
        depth = depth + 1
        for i := 0 ; i < length ; i++{
            node := queue.Remove(queue.Front()).(*TreeNode)
            if(node.Left == nil && node.Right == nil){
                return depth
            }
            if(node.Left != nil){
                queue.PushBack(node.Left)
            }
            if(node.Right != nil){
                queue.PushBack(node.Right)
            }
        }
    }

222.完全二叉树的节点个数

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func countNodes(root *TreeNode) int {
    ss := list.New()
    count := 0
    if root == nil {
        return count
    }
    ss.PushBack(root)
    for ss.Len() > 0{
        length := ss.Len()
        for i := 0 ; i < length ; i++{
            node := ss.Remove(ss.Front()).(*TreeNode)
            count++
            if(node.Left != nil){
                ss.PushBack(node.Left)
            }
            if(node.Right != nil){
                ss.PushBack(node.Right)
            }
        }
    }
    return count
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值