Fork me on GitHub

LeetCode之104. Maximum Depth of Binary Tree

--------------------------------

 

递归遍历即可

 

AC代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
}

 

题目来源: https://leetcode.com/problems/maximum-depth-of-binary-tree/

posted @ 2016-10-24 02:39  CC11001100  阅读(139)  评论(0编辑  收藏  举报