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 ...
随机推荐
- onMeasure实例分析
本文转自:http://blog.csdn.net/u012604322/article/details/17097105 上面这个两个视图是Android API中没有给出来的但 ...
- 【刷题】BZOJ 1061 [Noi2008]志愿者招募
Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完 ...
- mysql source导入多个sql文件
mysql>use dbtest; mysql>set names utf8; mysql>source D:/mysql/all.sql; 通过source命令导入多个文件,可以新 ...
- Long与long的区别
Java的数据类型分两种:1.基本类型:long,int,byte,float,double,char2. 对象类型(类): Long,Integer,Byte,Float,Double,Char,S ...
- Error: cannot allocate vector of size 88.1 Mb问题
这几天训练模型运行代码的时候,老是提示我说:Error: cannot allocate vector of size 88.1 Mb,只知道分配空间不足. 下面是查资料看到的一些回答: 一.这个是R ...
- bzoj 2809
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4519 Solved: 2329[Submi ...
- Python之paramiko模块和SQL连接API
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- GO_10:GO语言基础之error
Go错误处理 Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } ...
- P2015 二叉苹果树
P2015 二叉苹果树 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接 ...
- day15 接口与异常
接口 是一种独立于类的新机制,它关注的是行为. 接口的意义就体现在——让没有继承关系的类共享这些行为,各有各的具体实现. 设计上:当我们发现一个或多个类的方法相同,又没有继承关系,则考虑接口. 命名: ...