美文网首页
104. Maximum Depth of Binary Tre

104. Maximum Depth of Binary Tre

作者: namelessEcho | 来源:发表于2017-09-21 17:08 被阅读0次

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.

递归就好了,注意 min不可以这么解,因为这个方法返回null 为0;题目要求的是到叶子结点的。

/**
 * 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) {
        return find(root);
    }
    private int find (TreeNode root)
    {
        if(root==null)return 0;
        return Math.max(find(root.right),find(root.left))+1;
    }
}

相关文章

网友评论

      本文标题:104. Maximum Depth of Binary Tre

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