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 ...
随机推荐
-
关于js实现分页效果的简单代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
-
auto(c++11)
C++primer 第五版,第三章出现了此段程序,求解读附源码:代码1:#include<iostream>#include<string>using namespace st ...
-
找不到库文件地址,修改修改方法framework
直接双击地址行修改
-
CSS如何实现数字分页效果
代码实例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
-
canvas动画气球
canvas小球的动画我用canvas画布实现的小球动画效果,可以参考下 我用canvas画布实现的小球动画效果,可以参考下 我用canvas画布实现的小球动画效果,可以参考下 我用canvas画布实 ...
-
SpringCloud学习系列汇总
Spring Cloud常用组件使用汇总 使用SpringBoot2.0.3整合SpringCloud 服务注册与发现Eureka 自定义Eureka集群负载均衡策略 如何使用高可用的Eureka F ...
-
Collection<;T>; 的一个坑
当前所在的公司偏好使用 Collection<T>(System.Collections.ObjectModel), 这货比起List<T>不仅少了很多实用方法, 而且还有一个 ...
-
Android 5.0以上Material Design 沉浸式状态栏
偶然在知乎上看到这个问题,Android 5.0 如何实现将布局的内容延伸到状态栏,之前也见过多个应用的这个功能,但是知乎上的答案却没有一个真正实现此功能的一类是把标题栏设置App主题颜色,一类是提取 ...
-
[转]教你十分钟下载并破解IntelliJ IDEA(2017)
来源:http://www.itwendao.com/article/detail/400687.html 温馨提示:IntelliJ IDEA(2017)需要安装JDK8以上才能运行 如果你是JDK ...
-
ios12 siri 语音识别
原理:先用系统的录音器录音,让后让siri识别语音转文字 第一步 :在项目plist文件添加授权如图 第二步:导入头文件,添加协议#import <Speech/Speech.h>#imp ...