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 ...
随机推荐
- 使用SQLyog连接MySQL数据库
[学习笔记]使用SQLyog连接MySQL数据库 一.使用SQLyog创建数据库用来管理学生信息 复制代码 1 #创建数据库student 2 DROP DATABASE IF EXISTS Mys ...
- 基于c的简易计算器一
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h&g ...
- 5W2H方法
5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏 ...
- 【hihocoder编程练习赛9】闰秒
题目链接 #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h&g ...
- Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...
- HDU.1850 being a good boy in spring festival (博弈论 尼姆博弈)
HDU.1850 Being a Good Boy in Spring Festival (博弈论 尼姆博弈) 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 #include <bit ...
- 使用apt-mirror搭建debian本地仓库
apt-mirror能够将官方镜像下载到本地,并保证目录结构与其一致,但是不能对镜像仓库进行修改.如果想要修改镜像仓库,需要使用reprepro. 1.安装apt-mirror # aptitude ...
- 轻量高效的开源JavaScript插件和库 【转】
图片 布局 轮播图 弹出层 音频视频 编辑器 字符串 表单 存储 动画 时间 其它 加载器 构建工具 测试 包管理器 CDN 图片 baguetteBox.js - 是一个简单易用的响应式图像灯箱效果 ...
- CH暑假欢乐赛 SRM 07 天才麻将少女KPM(DP+treap)
首先LIS有个$O(n^2)$的DP方法 $f(i,j)$表示前i个数,最后一个数<=j的LIS 如果$a_i!=0$则有 如果$a_i=0$则有 注意因为$f(i-1,j)\leq f(i-1 ...
- python print end=' ' 不换行
python3.x 实现print 不换行 python中print之后是默认换行的,是因为其默认属性 end 默认值为"\n"(\n为换行符). 做练习99乘法表时不想换行,改变 ...