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. 零基础快速掌握Python系统管理视频课程【猎豹网校】

    点击了解更多Python课程>>> 零基础快速掌握Python系统管理视频课程[猎豹网校] 课程目录 01.第01章 Python简介.mp4 02.第02章 IPython基础.m ...

  2. 删除Zend Studio项目

    导入了过大的项目,导致很卡,且Close Project和Delete操作不了,一直无响应. 调整项目目录下的隐藏文件夹,删除对应项目: E:\www\.metadata\.plugins\org.e ...

  3. 笔记1 python入门学习笔记

    目录 官方手册 菜鸟站手册地址: python的运行方法 注释 小技巧: input()接收用户输入的内容(默认为字符串) print() 运算符 is 是判断两个标识符是不是引用自一个对象 all和 ...

  4. HDU 6156 回文 数位DP(2017CCPC)

    Palindrome Function Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Ot ...

  5. 动态规划:HDU-1398-Square Coins(母函数模板)

    解题心得: 1.其实此题有两种做法,动态规划,母函数.个人更喜欢使用动态规划来做,也可以直接套母函数的模板 Square Coins Time Limit: 2000/1000 MS (Java/Ot ...

  6. Leetcode 700. 二叉搜索树中的搜索

    题目链接 https://leetcode.com/problems/search-in-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点 ...

  7. Python高级主题:Python ABC(抽象基类)

    #抽象类实例 作用统一规范接口,降低使用复杂度.import abcclass Animal(metaclass = abc.ABCMeta): ##只能被继承,不能实例化,实例化会报错 @abc.a ...

  8. S变换

    哈哈,这两天在整理时频分析的方法,大部分参考网上写的比较好的资料,浅显易懂,在这谢过各位大神了! 今天准备写下S变换,由于网上资料较少,自己尝试总结下,学的不好,望各位多多指导 由前面的文章可知,傅里 ...

  9. Struts之上传

    上传的jsp写法: <tr> <td width="50%" align="left">软件上传: <input type=&qu ...

  10. ogre3D学习基础15 -- 创建BSP Scene Manager

    BSP(binary-space partitioning) Scene Manager(二叉空间分割)场景管理器比较适合用于室内场景. 第一,添加框架代码如下 #include "Exam ...