LeetCode-111.二叉树的最小深度(相关话题:深度优先)
2018-10-19 18:05
519 查看
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
[code] 3 / \ 9 20 / \ 15 7
返回它的最小深度 2.
Java代码:
[code]class Solution { public int minDepth(TreeNode root) { if(null == root) return 0; if(null == root.left && null == root.right) return 1; if(null == root.left) return 1 + minDepth(root.right); if(null == root.right) return 1 + minDepth(root.left); return 1 + Math.min(minDepth(root.left), minDepth(root.right)); } }阅读更多
相关文章推荐
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- LeetCode-113.路径总和II(相关话题:深度优先)
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- leetcode 111-二叉树最小深度
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- LeetCode-126.单词接龙II(相关话题:Dijkstra算法+深度优先)
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- leetcode 310. Minimum Height Trees 图的构建+最小高度树+DFS深度优先遍历+拓扑排序
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
- LeetCode—二叉树的最大最小深度
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
- Leetcode 111. Minimum Depth of Binary Tree 二叉树最小深度
- Leetcode Minimum Depth of Binary Tree 二叉树最小深度
- LeetCode-127.单词接龙(相关话题:广度优先)
- leetcode 687. Longest Univalue Path 二叉树最长相等路径+ 深度优先遍历DFS