104Maximum Depth of Binary Tree

来源:互联网 发布:oracle sql优化 编辑:程序博客网 时间:2024/05/01 14:02
/**
 * 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;
int leftDepth = 0, rightDepth = 0;
if(root.left!=null){
leftDepth = maxDepth(root.left);
}

if(root.right!=null){
rightDepth = maxDepth(root.right);
}

return Math.max(leftDepth, rightDepth) + 1;
    }
}
0 0
原创粉丝点击