二叉树的最小深度
来源:互联网 发布:java什么叫并发 编辑:程序博客网 时间:2024/04/30 12:01
题目描述
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.
最小深度需要考虑到是否某个子树深度为0,如果有个节点某个子树为0,就要返回左右子树中深度的最大值,而不是最小值。
class Solution {public: int run(TreeNode *root) { if(root==NULL) return 0; int left=run(root->left); int right=run(root->right); if(left==0||right==0) return 1+left+right; return 1+min(left,right); }};
0 0
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- 二叉树的最小深度
- JavaScript_6th_BOM编程
- mybatis逆向工程的使用
- 抽象类接口总结
- iOS 10调用相册权限设置crash问题
- Spring上下文ApplicationContext初始化Bean和事件
- 二叉树的最小深度
- Presentation 常用的承接句——技术分享、学术报告串联全场不尴尬
- leetCode刷题归纳-Divide and Conquer(312. Burst Balloons)
- CMD获取所有连接过的WIFI密码
- 经典网络还是VPC,开发者作何选择?
- 【Linux】VMware Player安装VMwareTools
- 第四章 表达式
- 使用Redux DevTools浏览器插件调试redux
- 快速排序