java二叉树与递归算法
public class Solution {
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int maxDepth(TreeNode root) {
//递归终止的条件,节点为空递归就应该停止,如果没有递归终止条件不停的往下执行会栈溢出
if(root == null)
return 0;
//递归的过程,描述算法逻辑
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}