【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int l,r;
if (root == NULL) return ;
l = maxDepth(root->left);
r = maxDepth(root->right);
return max(l,r)+; //一行代码的写法!!!!
//return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}
};
非递归写法1(直接摘自其他博客):
int maxDepth(TreeNode *root)
{
if (root == NULL) return ;
stack<TreeNode *> gray;
stack<int> depth;
int out = ; gray.push(root);
depth.push();
while (!gray.empty()) {
TreeNode *tmp = gray.top();
int num = depth.top();
gray.pop();
depth.pop();
if (tmp->left == NULL && tmp->right == NULL) {
out = num > out ? num : out;
}
else {
if (tmp->left != NULL) {
gray.push(tmp->left);
depth.push(num + );
}
if (tmp->right != NULL) {
gray.push(tmp->right);
depth.push(num + );
}
}
}
return out;
}
非递归写法2(直接摘自其他博客):
int maxDepth(TreeNode *root)
{
if(root == NULL)
return ; int res = ;
queue<TreeNode *> q;
q.push(root);
while(!q.empty())
{
++ res;
for(int i = , n = q.size(); i < n; ++ i)
{
TreeNode *p = q.front();
q.pop(); if(p -> left != NULL)
q.push(p -> left);
if(p -> right != NULL)
q.push(p -> right);
}
} return res;
}
【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度的更多相关文章
- Leetcode 104. 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 (二叉树的最大深度)
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(二叉树最大深度) 解题思路和方法
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(二叉树深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 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-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- JSP 学习总结 03 核心组件 Servlet
1 Servlet 简绍 Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生 ...
- Elasticsearch通关教程(一): 基础入门
简介 Elasticsearch是一个高度可扩展的.开源的.基于 Lucene 的全文搜索和分析引擎.它允许您快速,近实时地存储,搜索和分析大量数据,并支持多租户. Elasticsearch也使用J ...
- python之路3-元组、列表、字典、集合
1.元组 特点:一旦创建,内容不可修改,又叫只读列表 a= ('wang','zhang','zhao') print(a.count('zhao')) print(a.index('wang')) ...
- monkey日志管理
日志管理作用 Monkey日志管理是Monkey测试中非常重要的一个环节,通过日志管理分析,可以获取当前测试对象在测试过程中是否会发生异常,以及发生的概率,同时还可以获取对应的错误信息,帮助开发定位和 ...
- Drag(拖拽)和Move(移动)两个脚本
Drag using System.Collections; using System.Collections.Generic; using UnityEngine; public class Dra ...
- 作业二:Git的安装与使用
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 分布式版本控制系统Git的安装与使用 1.下载安装配置用户名 ...
- Django+Vue打造购物网站(八)
购物车.订单管理和远程调试 添加商品到购物车 trade/serializers.py from rest_framework import serializers from goods.models ...
- 机器学习---线性回归(Machine Learning Linear Regression)
线性回归是机器学习中最基础的模型,掌握了线性回归模型,有利于以后更容易地理解其它复杂的模型. 线性回归看似简单,但是其中包含了线性代数,微积分,概率等诸多方面的知识.让我们先从最简单的形式开始. 一元 ...
- [bilibili]弹幕屏蔽列表
<filters> <item enabled="true">t=定单身</item> <item enabled="true& ...
- 扩展CRT +扩展LUCAS
再次感谢zyf2000超强的讲解. 扩展CRT其实就是爆推式子,然后一路合并,只是最后一个式子上我有点小疑惑,但整体还算好理解. #include<iostream> #include&l ...