美文网首页LeetCode每日一题
LeetCode每日一题: 二叉树的最大深度

LeetCode每日一题: 二叉树的最大深度

作者: Patarw | 来源:发表于2020-07-24 13:00 被阅读0次

思路、利用递归实现

利用递归遍历所有节点,到底了就回溯,太简单了,也没啥可说的,直接上代码把

  • 代码:
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;
}
}

相关文章

网友评论

    本文标题:LeetCode每日一题: 二叉树的最大深度

    本文链接:https://www.haomeiwen.com/subject/ppcjlktx.html