104. Maximum Depth of Binary Tre

2017-09-21  本文已影响0人  namelessEcho

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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读