Subject description:
Seeking to set the maximum depth of a binary tree,
The maximum depth is the number of nodes of the root of the tree to the leaf node farthest longest path.
Problem-solving ideas:
Recursive, recursive binary tree is commonly used method.
/**
* 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;
int maxnum = 1;
maxnum = Math.max(maxnum + maxDepth(root.left), maxnum + maxDepth(root.right));
return maxnum;
}
}