LeetCode每日一题: 二叉树的最大深度
2020-07-24 本文已影响0人
Patarw
思路、利用递归实现
利用递归遍历所有节点,到底了就回溯,太简单了,也没啥可说的,直接上代码把
- 代码:
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(root.left == null ? 0 : maxDepth(root.left),root.right == null ? 0 : maxDepth(root.right)) + 1;
}
}