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 {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root) {
if(root==null) return 0;
return Math.Max(MaxDepth(root.left),MaxDepth(root.right))+1;
}
}