当前位置: 首页 > article >正文

54.二叉树的最大深度

迭代

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int de=0;
        Queue<TreeNode> qu=new LinkedList<>();
        TreeNode tn;
        int le;
        qu.offer(root);
        while(!qu.isEmpty()){
            le=qu.size();
            while(le>0){
                tn=qu.poll();
                if(tn.left!=null){
                    qu.offer(tn.left);
                }
                if(tn.right!=null){
                    qu.offer(tn.right);
                }
                le--;
            }
            de++;
        }
        return de;
    }
}
class Solution(object):
    def maxDepth(self, root):
        if root is None:
            return 0
        de=0
        qu=collections.deque()
        qu.append(root)
        while qu:
            le=len(qu)
            while le>0:
                tn=qu.popleft()
                if tn.left:
                    qu.append(tn.left)
                if tn.right:
                    qu.append(tn.right)
                le-=1
            de+=1
        return de

递归

class Solution {
    public int maxDepth(TreeNode root) {
        int de=0;
        TreeNode nod=root;
        return Explore(root,de);
    }
    int Explore(TreeNode nod,int de){
        if(nod==null){
            return de;
        }
        de++;
        return Math.max(Explore(nod.left,de),Explore(nod.right,de));
    }
}
class Solution(object):
    def maxDepth(self, root):
        de=0
        nod=root
        def Explore(nod,de):
            if nod is None:
                return de
            de+=1
            return max(Explore(nod.left,de),Explore(nod.right,de))
        return Explore(root,de)


http://www.kler.cn/news/335109.html

相关文章:

  • 全栈开发从未如此轻松:Bolt.new 让 AI 助力编程体验
  • 药物识别与分类系统源码分享
  • c#代码介绍23种设计模式_19状态者模式
  • 【WPF开发】超级详细的“文件选择”(附带示例工程)
  • 华为手机连接蓝牙音响后播放声音小的问题分析
  • RCE+[伪协议综合]
  • 图像转3D视差视频:DepthFlow
  • Linux Cent7 已安装MySQL5.7.X,再安装MYSQL8.4.2
  • 视频加字幕免费软件哪个好用?详细介绍6款字幕编辑软件的优缺点!码住!
  • 网络威胁情报技术的进步
  • Watchdog Timers(WDT)
  • 【鼠鼠学AI代码合集#7】概率
  • Ubuntu换源
  • [Leetcode LCR188.][Medium]-买卖芯片的最佳时机-dp/状态压缩
  • 文件后缀名不见了怎么办?
  • 【微服务】服务调用 - OpenFeign(day6)
  • linux启用 IPv4 转发
  • 物理学基础精解【56】
  • 每日一练:最长湍流子数组
  • Lustre v6 介绍