二叉树的最小深度
2015-09-28 12:58
337 查看
容易 二叉树的最小深度
31%通过
给定一个二叉树,找出其最小深度。
二叉树的最小深度为根节点到最近叶子节点的距离。
您在真实的面试中是否遇到过这个题?
Yes
样例
给出一棵如下的二叉树:
1
/ \
2 3
/ \
4 5
这个二叉树的最小深度为 2
标签 Expand
深度优先搜索
/** * Definition of TreeNode: * 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 tag = 1; int minDepth(TreeNode *root) { // write your code here if(root == NULL) return 0; if(root->left == NULL) return minDepth(root->right)+1; if(root->right == NULL) return minDepth(root->left)+1; return min(minDepth(root->left)+1, minDepth(root->right)+1); } };
相关文章推荐
- mysql创建存储过程 定时调用
- 【Linux命令-文件管理类】chgrp命令和groups命令
- PL/SQL Developer ORA-12154: TNS: 无法解析指定的连接标识符
- 好老板VS坏老板
- win10怎么设置高级黑?win10简单粗暴的个性化设置教程
- Java中关于HashMap的元素遍历的顺序问题
- 起始
- LR调用VC++6.0动态链接库
- 一键U盘启动快捷方式
- java 初始化(上)
- 二叉树的最大深度
- mysql 定时执行存储过程
- react-native —— 在Windows下搭建React Native Android开发环境
- 二叉树的前序遍历
- 二叉树的中序遍历
- 数据结构实验的目的、要求和评分标准
- USB设备的插入检测
- 完美的CListCtrl控件自绘
- 数据库管理系统(DBMS)的概述
- 最简单的推送--uexGetui