美文网首页
算法记录 | Day14 二叉树(03)

算法记录 | Day14 二叉树(03)

作者: perry_Fan | 来源:发表于2022-11-09 19:54 被阅读0次

    【104. 二叉树的深度】

      public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftDepth = maxDepth(root.left);
            int rightDepth = maxDepth(root.right);
            return Math.max(leftDepth, rightDepth) + 1;
        }
    

    【559.n叉树的最大深度】

        /*递归法,后序遍历求root节点的高度*/
        public int maxDepth(Node root) {
            if (root == null) return 0;
    
            int depth = 0;
            if (root.children != null){
                for (Node child : root.children){
                    depth = Math.max(depth, maxDepth(child));
                }
            }
    
            return depth + 1; //中节点
        }  
    

    【111.二叉树的最小深度】

    相关文章

      网友评论

          本文标题:算法记录 | Day14 二叉树(03)

          本文链接:https://www.haomeiwen.com/subject/mgeatdtx.html