Maximum Depth of Binary Tree
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
int maxDepth = 0, depthLeft = 0, depthRight = 0;
if(root == null)
return 0;
maxDepth = 1;
if(root.left != null){
depthLeft += maxDepth(root.left);
}
if(root.right != null){
depthRight += maxDepth(root.right);
}
maxDepth += ((depthLeft > depthRight) ? depthLeft : depthRight);
return maxDepth;
}
}