zoukankan      html  css  js  c++  java
  • Maximum Depth of Binary Tree

    题目

    Given a binary tree, find its maximum depth.

    The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

    题解:

    与minimum代码几乎完全一样,只需要修改math.min为math.max

    public class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            return getMin(root);
        }
    
        public int getMin(TreeNode root){
            if (root == null) {
                return Integer.MIN_VALUE;
            }
    
            if (root.left == null && root.right == null) {
                return 1;
            }
    
            return Math.max(getMin(root.left), getMin(root.right)) + 1;
        }
    }

     用divide and conquer

    public class Solution {
        public int maxDepth(TreeNode root) 
        {
            if (root==null) return 0;
            int left=maxDepth(root.left);
            int right=maxDepth(root.right);
            return Math.max(left,right)+1;
            
        }
    }
  • 相关阅读:
    023 AQS--JUC的核心
    022 Future接口
    021 Callable接口
    020 线程的综合考虑
    019 线程协作
    命令,lldb,llvm,gdb,gcc,
    @class,import,
    arc,自动引用计数,
    写在哪里,
    40岁生日,
  • 原文地址:https://www.cnblogs.com/hygeia/p/4703660.html
Copyright © 2011-2022 走看看