【Leetcode】【Easy】Maximum Depth of Binary Tree
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.
递归解题思路:
①结点为空时,返回0;②当前结点的深度 = max(两个孩子结点各自最大深度)+ 1
/**
* 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 maxDepth(TreeNode *root) {
if (!root)
return ; return max(maxDepth(root->left),
maxDepth(root->right)) + ;
}
};
迭代的解法:
用queue先进先出的特性,按层遍历,最后返回遍历的层数。
class Solution {
public:
int maxDepth(TreeNode *root) {
if (!root) {
return ;
}
queue<TreeNode *> nodeQue;
nodeQue.push(root);
int levelNodesCnt;
int depth = ;
while (!nodeQue.empty()) {
depth++;
levelNodesCnt = nodeQue.size();
while (levelNodesCnt--) {
if (nodeQue.front()->left)
nodeQue.push(nodeQue.front()->left);
if (nodeQue.front()->right)
nodeQue.push(nodeQue.front()->right);
nodeQue.pop();
}
}
return depth;
}
};
附录:
C++ queue使用
【Leetcode】【Easy】Maximum Depth of Binary Tree的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...
- 【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 ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 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 ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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 ...
- 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 ...
随机推荐
- UVA_11624 Fire! 【BFS】
一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...
- Java正则表达式-捕获组
private Set<String> getCodes(String s) { Set<String> resultSet = new HashSet<>(); ...
- 二叉堆(小到大)-数据结构-JavaScript版
/** * Created by caoke on 2015/11/21. */ //二叉树 特点父节点比子节点小 var Tree2=function(){ //初始化 二叉树的子元素 this.c ...
- EntityFramework 并发处理
转载自:http://www.cnblogs.com/TianFang/p/4439215.html 什么是并发? 并发分悲观并发和乐观并发. 悲观并发:比如有两个用户A,B,同时登录系统修改一个文档 ...
- c# 屏蔽快捷键
前言 有时候开发会遇到这样一个需求,软件需要屏蔽用户的组合快捷键或某些按键,避免强制退出软件,防止勿操作等. 原理 1.要实现组合键,按键拦截,需要用到user32.dll中的SetWindowsHo ...
- 使用SVN进行源码管理
阅读目录: 1.SVN服务端配置 1.1 创建版本库 1.2 创建用户 1.3 设置用户权限 2.SVN客户端使用 2.1 向SVN服务器中导入源码 2.1.1 直接通过TortoiseSVN向SVN ...
- Ubuntu系统里如何正确卸载oracle-j2sdk1.6(图文详解)
问题来源 因为,我在安装clouder manager5.X的时候,默认是需要oracle-j2sdk1.7. 它4.X时,才是1.6的. 若你还是在用clouder manager4.X版本的话,需 ...
- [實現DDD] 第11章 工廠
創建過程中須考慮一些重要細節, 否則所創建的聚合將處於不正確的狀態, 使用適當的工廠方法可以確保這一點, 而客戶端只需輸入基本的參數(通常是值對象), 另外, 工廠能更好地表達出通用語言, 使團隊成員 ...
- nginx配置多域名
http{ # 第一个虚拟主机 server { listen 80; server_name aaa.domain.com; #access_log logs/host.access.log mai ...
- C语言中extern的用法--转
http://blog.sina.com.cn/s/blog_52deb9d50100ml6y.html 在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的, ...