二叉树的最小深度-LintCode
来源:互联网 发布:管家婆erp是什么软件 编辑:程序博客网 时间:2024/06/06 14:08
给定一个二叉树,找出其最小深度。
二叉树的最小深度为根节点到最近叶子节点的距离。
样例
给出一棵如下的二叉树:
这个二叉树的最小深度为 2
#ifndef C155_H#define C155_H#include<iostream>using namespace std;class TreeNode{public: int val; TreeNode *left, *right; TreeNode(int val) { this->val = val; this->left = this->right = NULL; }};class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int minDepth(TreeNode *root) { // write your code here if (root == NULL) return 0; else return minDepthRecur(root); } int minDepthRecur(TreeNode* node) { if (node == NULL) return INT_MAX; TreeNode *p =node; if (p->left == NULL&&p->right == NULL) return 1; else { return minVal(minDepthRecur(p->left), minDepthRecur(p->right)) + 1; } } int minVal(int a, int b) { return a < b ? a : b; }};#endif
阅读全文
0 0
- LintCode 二叉树的最小深度
- lintcode-二叉树的最小深度-155
- LintCode:二叉树的最小深度
- lintcode ----二叉树的最小深度
- Lintcode 二叉树的最小深度
- LintCode-二叉树的最小深度
- lintcode 二叉树的最小深度
- LintCode 二叉树的最小深度
- 二叉树的最小深度-LintCode
- lintcode 二叉树的最小深度
- LintCode 二叉树的最小深度
- lintcode 155 二叉树的最小深度
- LintCode 155-二叉树的最小深度
- 二叉树的最小深度-LintCode
- lintcode二叉树的最小深度
- lintcode——二叉树的最小深度
- lintcode(E) 二叉树最小深度
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- 常用的几个正则表达式式
- 如何优化cocos2d/x程序的内存使用和程序大小
- RDS MySQL InnoDB 锁等待和锁等待超时的处理
- java-入门
- PLSQL Developer连接远程oracle配置
- 二叉树的最小深度-LintCode
- Linux笔记--1.如何学习Linux
- 使用Servlet写一个单词查询构思
- Java学习路线
- kafka逻辑示意图以及命令
- 2017.8.14 文本生成器 失败总结
- 二叉排序树之哈夫曼树
- 蓝桥杯_算法训练_矩阵乘法
- 《大话数据结构》