LeetCode 104 二叉树的最大深度
2019-06-08 12:00
344 查看
题目描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
返回它的最大深度 3 。
分析
DFS策略。
DFS
沿着树的深度遍历树的节点,尽可能深的搜索树的分支。More:https://zh.wikipedia.org/wiki/%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2
代码
class Solution { public int maxDepth(TreeNode root) { int cnt = 0; if (root == null) return cnt; TreeNode p = root.left; TreeNode q = root.right; cnt = java.lang.Math.max(maxDepth(p), maxDepth(q)); return ++cnt; } }
相关文章推荐
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | 深度优先与广度优先深入理解
- [LeetCode-104] Maximum Depth of Binary Tree(二叉树最大深度)
- [LeetCode] 104 二叉树最大深度
- LeetCode104--二叉树最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- LeetCode 104--二叉树的最大深度
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
- 【LeetCode】[104] 二叉树的最大深度——“递归”
- 二叉树的最大深度(leetcode-104)
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- Leetcode 104 python 二叉树的最大深度
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode 104 Maximum Depth of Binary Tree(二叉树的最大深度)
- Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
- LeetCode-104.二叉树的最大深度(相关话题:递归)
- leetcode 104求二叉树的最大深度
- leetcode-腾讯精选练习(50 题)python #104 二叉树的最大深度
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- LeetCode写题历程——104.二叉树的最大深度
- LeetCode 104 二叉树的最大深度