C#LeetCode刷题之#104-二叉树的最大深度(Maximum Depth of Binary Tree)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4072 访问。
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
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.
Note: A leaf is a node with no children.
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4072 访问。
public class Program {
public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(2),
right = new TreeNode(3)
};
var res = MaxDepth(root);
Console.WriteLine(res);
res = MaxDepth2(root);
Console.WriteLine(res);
Console.ReadKey();
}
public static int MaxDepth(TreeNode root) {
//递归法,本示例无法使用尾递归
if(root == null) return 0;
var left = MaxDepth(root.left);
var right = MaxDepth(root.right);
return Math.Max(left, right) + 1;
}
public static int MaxDepth2(TreeNode root) {
//若为空,返回 0
if(root == null) return 0;
//定义结果
var res = 0;
//定义一个栈,存一个键值对,键为节点,值为节点的深度
var stack = new Stack<KeyValuePair<TreeNode, int>>();
//把根节点压入栈中,其深度为 1
stack.Push(new KeyValuePair<TreeNode, int>(root, 1));
//处理栈,条件为栈不为空,即当栈空时终止
while(stack.Any()) {
//弹出栈顶元素所记录
var top = stack.Pop();
//每次比较最大值并存入 res
res = Math.Max(res, top.Value);
//为左、右子节点的深度计数
if(top.Key.left != null) {
//若左子节点不为空,则将左子节点压入栈中,并将其深度值 +1
stack.Push(new KeyValuePair<TreeNode, int>(top.Key.left, top.Value + 1));
}
if(top.Key.right != null) {
//若右子节点不为空,则将右子节点压入栈中,并将其深度值 +1
stack.Push(new KeyValuePair<TreeNode, int>(top.Key.right, top.Value + 1));
}
}
//返回 res
return res;
}
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4072 访问。
2
2
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#104-二叉树的最大深度(Maximum Depth of Binary Tree)的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- [Swift]LeetCode104. 二叉树的最大深度 | 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 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 ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 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 ...
- [LintCode] 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练习题】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——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
随机推荐
- 使用Java带你打造一款简单的英语学习系统
[一.项目背景] 随着移动互联网的发展,英语学习系统能结构化的组织海量资料.针对用户个性需求,有的放矢地呈现给用户,从而为英语学习者提供便利,提升他们的学习效率. [二.项目目标] 1. 实现美观的界 ...
- linux 安装superset
背景说明 公司数据分析人员需要将日常监控分析数据进行可视化,在踩了一些坑之后,终于在业务环境中搭建成功superset,后续复现两次流程也是成功的,分享一波... 业务环境说明 操作系统:centos ...
- python3利用cryptography 进行加密和解密
我们的日常工作中,一定会遇到需要加密的数据,比如:密码.私密信息... ... 我们不仅要对他们进行加密,更需要对他们进行解密,因为毕竟我们的用户应该不会看得懂加密过后的字符串吧!!! 在python ...
- java面试题jvm字节码的加载与卸载
虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验,转换分析和初始化,最终形成可以被虚拟节直接使用的JAVA类型,这就是虚拟机的类加载机制. 类从被加载到虚拟机内存到卸载出内存的生命周期 ...
- row_number() over()排序功能说明
1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排 ...
- 普通list和树状list互转
import java.util.ArrayList; import java.util.List; public class TreeNode { private String id; privat ...
- webpack源码-打包资源输出到本地
webpack收集完依赖是怎么打包资源的呢? 入口compiler.js: this.applyPluginsParallel("make", compilation, err = ...
- SpringBoot2.x入门教程:引入jdbc模块与JdbcTemplate简单使用
这是公众号<Throwable文摘>发布的第23篇原创文章,收录于专辑<SpringBoot2.x入门>. 前提 这篇文章是<SpringBoot2.x入门>专辑的 ...
- Java之枚举类
有时候,变量的取值只在一个有限的集合内. 例如:pizza的大小只有小.中.大和超大这四种尺寸.当然,可以将这些尺寸分别编码为1.2.3.4或者S.M.L.X.但这样存在着一定的隐患.在变量中很有可能 ...
- 题解 洛谷 P4336 【[SHOI2016]黑暗前的幻想乡】
生成树计数的问题用矩阵树定理解决. 考虑如何解决去重的问题,也就是如何保证每个公司都修建一条道路. 用容斥来解决,为方便起见,我处理时先将\(n\)减了1. 设\(f(n)\)为用\(n\)个公司,且 ...