[leetcode] 21.Maximum Depth of Binary Tree

public class Solution {
    public int maxDepth(TreeNode root) {
        int count = 0;
        if(root==null)
            count = 0;
        else if(root.left==null && root.right==null)
            count = 1;
        else{
            int lmax = maxDepth(root.left)+1;
            int rmax = maxDepth(root.right)+1;
            count = Math.max(lmax,rmax);
        }
        return count;
    }
}

  树的高度为:左子树的高度与右子树高度二者较大的值,递归求解。

posted @ 2014-04-15 11:18  果汁果粒  阅读(112)  评论(0编辑  收藏  举报