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.

Hide Tags

Tree Depth-first Search

 

 
  简单的深度搜索
#include <iostream>
using namespace std;
/**
* Definition for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
int maxRet;
int maxDepth(TreeNode *root) {
maxRet=;
int curDep=;
help_f(root,curDep);
return maxRet;
}
void help_f(TreeNode * root,int &curDep)
{
if(root==NULL) return ;
curDep++;
help_f(root->left,curDep);
help_f(root->right,curDep);
curDep--;
if(root->left==NULL&&root->right==NULL){
if(maxRet<curDep)
maxRet=curDep;
}
}
}; int main()
{
return ;
}

[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索的更多相关文章

  1. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. leetcode 104 Maximum Depth of Binary Tree(DFS)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

  5. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. Leetcode Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

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

随机推荐

  1. 两个list缩进为一个list,python

    # w_list = ['a', 'b', 'c', 'd'] # e_list = ['c', 'd', 'b', 'a'] w_list = ['a', 'b', 'c', 'd', 'ff', ...

  2. vmware 开机自动启动

    vmware开机自动启动, 可以使用vmrun命令. 1. 首先在“我的电脑”-“属性”-“高级”-“环境变量”-“PATH”中添加vmware路径,如:C:\Program Files (x86)\ ...

  3. Java课堂作业详解

    今天的Java课堂留下了一个作业:使用Eclipse编写一个程序,使输入的两个数进行加和,并且输出他们的和.对于这个题目,我们首先可以把它分解成为三个不同的小步骤 第一步就是输入这两个数,因为我们无需 ...

  4. 初学Python不知道做什么项目好?来看看练手项目如何?

    对于初学者来说,在学习编程的初期,由于基础知识点的学习是比较无聊的,所以大家可能会有所反感,为了减弱大家的反感,我给大家带来一个简单的小项目——实现屏保计时器,这个项目就算是刚学Python的小伙伴, ...

  5. javascript隐藏和显示元素以及清空textarea

    当前希望写一个单选框,选中“paste”则显示粘贴框,选中“upload”则提示选择文件. 因为这两种情况只是显示不同,所以只需要用javascript来进行显示和隐藏. 最后的结果大概这样: 初始时 ...

  6. 【STM32】IIC的基本原理(实例:普通IO口模拟IIC时序读取24C02)(转载)

     版权声明:本文为博主原创文章,允许转载,但希望标注转载来源. https://blog.csdn.net/qq_38410730/article/details/80312357 IIC的基本介绍 ...

  7. DFS:POJ1190-生日蛋糕(基础搜索)

    生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. ...

  8. 动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击

    题目: 1010: 魔兽争霸之最后的反击                                                                         Time Li ...

  9. 笔记-算法-KMP算法

    笔记-算法-KMP算法 1.      KMP算法 KMP算法是一种改进的字符串匹配算法,KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一 ...

  10. Android开发——JVM、Dalvik以及ART的区别

    )预编译也可以明显改善电池续航,因为应用程序每次运行时不用重复编译了,从而减少了 CPU 的使用频率,降低了能耗.