LeetCode——minimum-depth-of-binary-tree 二叉树的最小深度
2019-04-07 15:05
316 查看
LeetCode——minimum-depth-of-binary-tree 二叉树的最小深度
题目描述:
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.
题目分析:
深度和广度皆可。深度用递归,广度用队列实现。
下面代码用深度搜索,递归实现
AC代码:
class Solution { public: int run(TreeNode *root) { if(root==NULL)//空树返回0 return 0; if(root->left&&root->right)//如果有左右孩子,返回左右子树的最小深度+1 return min(run(root->left),run(root->right))+1; else if(root->left)//如果只有左孩子,返回左子树深度+1 return run(root->left)+1; else if(root->right)//如果只有右孩子,返回右子树深度+1 return run(root->right)+1; else return 1;//只有根节点,没有左右孩子返回1 } };
相关文章推荐
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- LeetCode Minimum Depth of Binary Tree 最小深度二叉树
- leetcode:minimum-depth-of-binary-tree(二叉树的最小深度)
- [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- Leetcode Minimum Depth of Binary Tree 二叉树最小深度
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- 【LeetCode】Minimum Depth Of Binary Tree二叉树的最小深度 - Easy
- Minimum Depth of Binary Tree 求二叉树最小高度/深度@LeetCode
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
- 【leetcode】Minimum Depth of Binary Tree 二叉树的最小深度 (JAVA)
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- LeetCode:minimum-depth-of-binary-tree(二叉树最小深度)
- Minimum Depth of Binary Tree 二叉树的最小深度
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
- Minimum Depth of Binary Tree 二叉树的最小深度