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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

随机推荐

  1. java异常处理-finally中使用return和throw语句

    java异常语句中的finally块通常用来做资源释放操作,如关闭文件.关闭网络连接.关闭数据库连接等.正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java ...

  2. HDU1565_方格取数(1)

    给一个数字方阵,你要从中间取出一些数字,保证相邻的两个数字不同时被取出来,求取出来的最大的和是多少? 建立图模型,对于行列的和为奇数的格子,建立一条从原点到达这个点的边,对于行列和为偶数的格子,建立一 ...

  3. Nginx在Linux上的安装和配置

    链接:http://www.cnblogs.com/wbyp/p/7737224.html

  4. installns

    installns 将升级文件NSVPX-NCore_build-12.1-48.13_nc_64.tgz,上传至设备的“/var/nsinstall”目录下. 在命令行中执行以下命令,查看升级脚本使 ...

  5. BZOJ2159 Crash 的文明世界 【第二类斯特林数 + 树形dp】

    题目链接 BZOJ2159 题解 显然不能直接做点分之类的,观察式子中存在式子\(n^k\) 可以考虑到 \[n^k = \sum\limits_{i = 0} \begin{Bmatrix} k \ ...

  6. 【bzoj3730】 震波

    http://www.lydsy.com/JudgeOnline/problem.php?id=3730 (题目链接) 题意 给出一棵树,每个节点又一个权值.两个操作,询问距离节点${x}$不超过${ ...

  7. spark 调优——基础篇

    开发调优 调优概述 Spark性能优化的第一步,就是要在开发Spark作业的过程中注意和应用一些性能优化的基本原则.开发调优,就是要让大家了解以下一些Spark基本开发原则,包括:RDD lineag ...

  8. CentOS7防火墙firewalld使用

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  9. 关于 DjangoUeditor 上传图片图片失败,csrf token missing or incorrect 的解决办法

    Forbidden (CSRF token missing or incorrect.): /ueditor/controller/ [27/Jun/2017 23:49:25] "POST ...

  10. GO_01:Mac之Go语言Idea环境配置

    声明:本人所使用的是Mac Pro 安装开始 1. 首先将 GO 基础组件安装好,就好似 java 中的 jdk.当然,安装的时候需要到官网去下载,这一步难倒了好多无法FQ的同学们,故这里我将我这边下 ...