zoukankan      html  css  js  c++  java
  • [leetCode]104.二叉树的最大深度

    解法一 递归

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null) return 0;
            else{
                int leftHeight = maxDepth(root.left);
                int rightHeight = maxDepth(root.right);
                return Math.max(leftHeight,rightHeight)+1;
            }
        }
    }
    

    解法二 广优先搜索

    首先加入根结点,每次弹出一个结点,加入左右子节点并跟新每个结点的深度,使用一个遍历不断更新最大深度

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            Queue<Pair<TreeNode, Integer>> stack = new LinkedList<>();
            if(root != null){
                stack.add(new Pair(root, 1));
            }
            int depth = 0;
            while(!stack.isEmpty()){
                Pair<TreeNode, Integer> current = stack.poll();
                root = current.getKey();
                int currentMaxH = current.getValue();
                if(root != null){
                    depth = Math.max(currentMaxH, depth);
                    stack.add(new Pair(root.left,depth + 1));
                    stack.add(new Pair(root.right,depth + 1));
                }
            }
            return depth;
        }
    }
    

    每次往栈中添加一层结点,遍历取出该层结点添加下一层非空结点,每层遍历完成,层级+1

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            if(root == null) return 0;
            Queue<TreeNode> stack = new LinkedList<>();
            stack.add(root);
            int level = 0;
            while(!stack.isEmpty()){
                int levelSize = stack.size();//每一层的节点数
                for(int i = 0; i < levelSize; i++){//每次弹出一层结点,添加下一层结点
                    root  = stack.poll();
                    if(root.left!=null){
                        stack.add(root.left);
                    }
                    if(root.right!=null){
                        stack.add(root.right);
                    }
                }
                level++;
            }
            return level;
        }
    }
    
  • 相关阅读:
    动态SQL语句
    Mybatis配置和基于配置的使用
    JQuery封装的ajax、ajax上传文件、JSON对象
    Jsp生命周期、Jsp的使用、JSP隐式对象、EL表达式、JSTL
    原生Ajax
    Servlet上传文件、会话跟踪、Cookies和session的使用及其常用方法
    ResponseBodyAdvice拦截Controller方法默认返回参数,统一处理返回值/响应体
    钉钉机器人
    花瓶安装和使用
    方法入参检测工具类 spring自带org.springframework.util.Assert 通用类
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13860014.html
Copyright © 2011-2022 走看看