求二叉树深度

101 阅读1分钟
/**
 * 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 recurr(root);
    }
    int recurr(TreeNode root){
        if(root==null) return 0;
        int l = recurr(root.left)+1;
        int r = recurr(root.right)+1;
        return Math.max(l,r);
    }
}