/**
* 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) {
if(root==null){
return 0;
}
return maxDep(root, 1);
}
private int maxDep(TreeNode node, int pDepth){
int leftDep = pDepth;
int rightDep = pDepth;
int cDepth = ++pDepth;
if(node.left!=null){
leftDep = maxDep(node.left, cDepth);
}
if(node.right!=null){
rightDep = maxDep(node.right, cDepth);
}
return leftDep>rightDep?leftDep:rightDep;
}
}