求二叉树深度

·  阅读 61
/**
 * 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);
    }
}
复制代码
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改