[LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
来源:互联网 发布:姆潘巴现象知乎 编辑:程序博客网 时间:2024/06/14 01:58
Given a binary tree, find its Minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
本题就是求解二叉树的深度:
递归解法:
(1)如果二叉树为空,二叉树的深度为0
(2)如果二叉树不为空,二叉树的深度 = min(左子树深度, 右子树深度) + 1
(3)需要注意的是斜二叉树情况,当节点的左子树深度为0或者节点的右子树深度为0.
采用DFS深度优先遍历,我们只要叶子节点。
/** * Definition for a binary tree node. */struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;};int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 1; int leftDepth = minDepth(root->left); int rightDepth = minDepth(root->right); if (leftDepth == 0) return rightDepth + 1; else if (rightDepth == 0) return leftDepth + 1; else return leftDepth < rightDepth ? (leftDepth +1 ) : (rightDepth +1); }
1 0
- LeetCode Minimum Depth of Binary Tree 最小深度二叉树
- Leetcode Minimum Depth of Binary Tree 二叉树最小深度
- LeetCode:minimum-depth-of-binary-tree(二叉树最小深度)
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- LeetCode OJ 之 Minimum Depth of Binary Tree(二叉树的最小深度)
- LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- Minimum Depth of Binary Tree 求二叉树最小高度/深度@LeetCode
- leetCode 111.Minimum Depth of Binary Tree(二叉树最小深度) 解题思路和方法
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- Leetcode 111. Minimum Depth of Binary Tree 二叉树最小深度
- [LeetCode]111. Minimum Depth of Binary Tree--二叉树的最小深度
- 【LeetCode】Minimum Depth Of Binary Tree二叉树的最小深度
- 修改mysql数据库为utf8
- DOCTYPE
- win0 环境下局域网 搭建FTP服务器
- 编程中遇到的Bugs
- DML、DDL、DCL
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- 大整数乘法---分治
- MySQL慢日志查询用法
- 深入理解Java内存模型(六)——final
- Python学习手册 - 11
- 16 复杂链表的复制
- 从僵尸网络追踪到入侵检测 第8章 Honeyd配置文件使用
- 二叉树三种遍历(递归及非递归)的实现
- 修改mysql数据库的用户名和密码