LeetCode 111:Minimum Depth of Binary Tree
2015-12-28 16:16
369 查看
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.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
//递归方法求二叉树的最小深度 //注意判断树的深度应该到叶子节点,也就是左右子结点都为空的那个结点 class Solution { public: int minDepth(TreeNode *root) { if (root == NULL) return 0; if (root->left == NULL) return minDepth(root->right) + 1; else if (root->right == NULL) return minDepth(root->left) + 1; else return min(minDepth(root->left), minDepth(root->right)) + 1; } };
相关文章推荐
- Android学习路线指南
- 设计原则之迪米特法则
- Android 高仿腾讯旗下app的 皮肤加载技术
- 12,13,wedfrom 基本控件
- R语言练习(一)
- 循环语句(for语句的用法)
- TsinghuaX: 00740043X C++语言程序设计基础 第三章提纲
- OA系统的左侧菜单栏的菜单项的处理
- js判断复选框的数量
- MFC 使用Python脚本语言
- Java代码优化
- Apache 伪静态模块mod_rewrite的介绍与使用
- 基于Linux与Busybox的Reboot命令流程分析
- 用AFN时报错
- stl map用法和make_pair函数和pair结构体
- 描绘柱状图
- Windows 7安装mysql-5.6.15-winx64解压版
- C# 实现 任意多边形切割折线算法
- studio快捷键
- Process 'command 'D:\Program Files\Java\jdk\bin\java. 解决办法