Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/*
* 得到每个节点的depth,然后一层层向上返回高度
*/
public int maxDepth(TreeNode root) {
int Depth1 = 0;
int Depth2 = 0;
if(root == null){
return 0;
}else
{
Depth1 = maxDepth(root.left);
Depth2 = maxDepth(root.right);
if(Depth1 > Depth2){
return Depth1 + 1;
}else
{
return Depth2 + 1;
}
}
}
}