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 ...
随机推荐
- PHP中测试in_array、isset、array_key_exists性能
测试in_array.isset.array_key_exists性能.自己写的简易测试代码: ini_set('display_errors',true); error_reporting(E_AL ...
- LeetCode 717. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented by one bit 0. The second char ...
- linux eclipse add desktop shortcut with root permission
gedit ~/.local/share/applications/opt_eclipse.desktop sudo apt-get install gksu [Desktop Entry] Type ...
- BZOJ 4520: [Cqoi2016]K远点对
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 638 Solved: 340[Submit][Status ...
- 【Learning】辛普森积分
辛普森积分 这种积分法很暴力:只要求你实现出函数求值\(f(x)\). 使用辛普森积分,我们可以求出函数一段区间\([l,r]\)的近似积分.记\(mid=\frac{l+r}2\),有: \[ \i ...
- logstash5生成init脚本后台启动
默认情况使用rpm包安装完logstash之后没有启动脚本,这一点我觉得算是开发不够彻底.官网给了一个脚本,需要根据不同的系统版本生成对应的启动脚本,而且官网没有给明使用方法,对于新用户来说算是个坑, ...
- php高效遍历文件夹、高效读取文件
/** * PHP高效遍历文件夹(大量文件不会卡死) * @param string $path 目录路径 * @param integer $level 目录深度 */ function fn_sc ...
- ImageView的android:scaleType各属性含义(zz)
android:scaleType是控制图片如何resized/moved来匹对ImageView的size.ImageView.ScaleType / android:scaleType值的意义区别 ...
- Kafka 接受数据并消费到hbase数据库
一. 1.生产者 产生数据 package kafakaTohbase; import java.util.Properties; import kafka.javaapi.producer.Prod ...
- Django JSON 时间
在views.py中导入: from django.core.serializers.json import DjangoJSONEncoder 在返回JSON数据时调用: return HttpRe ...