[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
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.
#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,深度搜索的更多相关文章
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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 ...
- 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 ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- [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 ...
- 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 ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- 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 ...
随机推荐
- inotifywait实时监控文件目录
一.inotify简介 inotify 是一种强大的.细粒度的.异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性.读写属性.权限属性.创建删除.移动等操作,也可以监控文件 ...
- 麦子学院python开发全套完整无加密课程
点击了解更多Python课程>>> 麦子学院python开发全套完整无加密课程 第一阶段:Python基础准备 1.Web前端开发之HTML+CSS基础入门 2.Javascript ...
- 【PHP】php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- web项目中无法开启或404
404找不到页面,可能是spring的bean自动注入有了问题,例如org.springframework.beans.factory.BeanCreationException:可以检查配置文件的s ...
- 基于Ajax提交formdata数据、错误信息展示和局部钩子、全局钩子的校验。
formdata重点: 实例化FormData这个类 循环serializeArray可以节省代码量 图片要用$('#id')[0].files[0]来获得 加上contentType:false和p ...
- error LNK2001: unresolved external symbol ___CxxFrameHandler3
Q: VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol ___CxxFrameHandler3 A: ...
- ogre3D学习基础19 --- 材质的继承,纹理的滚动与旋转
以上一节为基础,废话不多说. 首先新增一个节点,用于比较显示 //新增一个节点 ent = mSceneMgr->createEntity("Quad"); ent-> ...
- C++指针详解 (转)
C++指针详解 指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区, ...
- 聊聊、Spring 数据源
平时开发中我们每天都会跟数据库打交道,页面上显示的数字,图片,语音,等等都存在某个地方,而我们就是要从那个地方拿到我们想要的.现在存储数据的方式越来越多,多种多样,但用的最多的还是关系数据库.Spri ...
- Python面相对象之类里面常用的装饰器(3)
在类里面,可以设置类的全局变量,也就是静态字段,让实例化的所有对都具有该属性 class god: country = 'china'#这个字段在类里面保存,只有一份,叫静态字段,表示每个对象具有的属 ...