2016.6.26——Maximum Depth of Binary Tree
Maximum Depth of Binary Tree
本题收获
1.树时使用递归
2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件。
题目:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
题目中说:最短路径的定义是从根节点到最近子节点的最短路径,必须是子节点[1,2]正确输出应该是2,如果不判断左子节点和右子节点输出就会变成1.
[1,2]: [1,2,3]
1 1
/ /
2 2
/
3
思路:
我的思路:首先判断root是否等于NULL,然后直接使用递归。
leetcode:1.判断root是否为NULL,2.判断root->left是否为空,3.判断root->right是否为空,4.利用递归,每次加1计算长度。
代码:
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return ;
if (root->left == NULL) return minDepth(root->right) + ;
if (root->right == NULL) return minDepth(root->left) + ;
return min(minDepth(root->left), minDepth(root->right)) + ;
}
};
出错代码1:
没有判断左子节点和右子节点为空的情况,若不判断在1,2这种情况就返回1,但是正确应该是返回2,题目是说从根节点到子节点。
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return ;
return min(minDepth(root->left), minDepth(root->right)) + ;
}
};
我的测试代码:
// Minimum Depth of Binary Tree.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "malloc.h"
#include "algorithm"
using namespace std; struct TreeNode
{
int val;
TreeNode *left, *right;
TreeNode(int x) :val(x), left(NULL), right(NULL){};
}; void creatTree(TreeNode* &T)
{
int data;
cin >> data;
if (data == -)
{
T = NULL;
}
else
{
T = (TreeNode*)malloc(sizeof(TreeNode));
T->val = data;
creatTree(T->left);
creatTree(T->right);
} } class MyClass
{
public:
int minDepth(TreeNode* root)
{ if (root == NULL) return ;
if (root->left == NULL) return minDepth(root->right) + ; //leetcode上1,2这样最短是返回2,而不是1
if (root->right == NULL) return minDepth(root->left) + ; //题目中有说是从根节点到最近的叶子节点,必须时到叶子节点啊
return min(minDepth(root->left), minDepth(root->right)) + ; } }; int _tmain(int argc, _TCHAR* argv[])
{
TreeNode* root = NULL;
int m = ;
creatTree(root);
MyClass solution;
m = solution.minDepth(root);
cout << m << endl;
system("pause");
return ;
}
2016.6.26——Maximum Depth of Binary Tree的更多相关文章
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- android之layer-list
效果图: 实现代码: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:and ...
- Java容器深入浅出之HashSet、TreeSet和EnumSet
Java集合中的Set接口,定义的是一类无顺序的.不可重复的对象集合.如果尝试添加相同的元素,add()方法会返回false,同时添加失败.Set接口包括3个主要的实现类:HashSet.TreeSe ...
- get mobile http request in PC & fiddler4
get mobile http request in PC 如何在 pc 上抓取 手机的 http 请求 Fiddler 提琴手 https://www.telerik.com/download/fi ...
- npm 镜像修改
1, 修改 下载仓库为淘宝镜像 npm config set registry http://registry.npm.taobao.org/ 2, 如果要发布自己的镜像需要修改回来 npm co ...
- Swagger实现API文档功能
介绍: wagger也称为Open API,Swagger从API文档中手动完成工作,并提供一系列用于生成,可视化和维护API文档的解决方案.简单的说就是一款让你更好的书写API文档的框架. 我们为什 ...
- 【bzoj3930】 CQOI2015—选数
http://www.lydsy.com/JudgeOnline/problem.php?id=3930 (题目链接) 题意 求在${[L,R]}$中选出${n}$个数,可以相同,使得它们的${gcd ...
- 构建工具----gradle---可能遇到的问题----Could not reserve enough space for object heap
Could not reserve enough space for object heap 意思是 jvm的设置内存不足以运行gradle命令了. 分为两种情况,解决的方法也不同. .10/user ...
- 解题:BZOJ 2673 World Final 2011 Chips Challenge
题面 数据范围看起来很像网络流诶(滚那 因为限制多而且强,数据范围也不大,我们考虑不直接求答案,而是转化为判定问题 可以发现第二个限制相对好满足,我们直接枚举这个限制就可以.具体来说是枚举所有行中的最 ...
- 阿里云 邮件发送(Python)
#coding:utf8 from smtplib import SMTP_SSL from email.header import Header from email.mime.text impor ...
- boost::asio::io_service类
大部分使用Boost.Asio编写的代码都会使用几个io_service的实例.io_service是这个库里面最重要的类:它负责和操作系统打交道,等待所有异步操作的结束,然后为每一个异步操作调用其完 ...