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 ...
随机推荐
- 史上最全的 jmeter 获取 jdbc 数据使用的四种方法
jmeter使用jdbc协议获取数据库中数据,很多人都会用,因为大家在做测试的时候,有时候需要大量的用户进行登录,获取需要数据库中真实的数据用于测试,所以常常会用jdbc来获取数据库数据. 那从数据库 ...
- 关于C# winform唤起本地已安装应用程序(测试win10,win7可用)
想要唤起本地已安装应用程序,我想到的有三种可行的方法: 第一种就是打开本地的快捷方式(有的应用可能没有快捷方式,但这种方法效率最高,可配合其他方法使用),快捷方式分为本地桌面快捷方式和开始菜单中的快捷 ...
- NIO入门之多路复用选择器Selector
简介 Selector 是 java.nio.channels 包下的重要组件,阅读本文可以带你了解常用的 API.本文中把 Channel 翻译成信道,按照个人习惯也可以称作是通道.管道. Sele ...
- 搭建mysql NDB集群
NDB群集安装 介绍 https://dev.mysql.com/doc/refman/8.0/en/mysql-cluster-basics.html NDBCLUSTER (也称为NDB)是一种内 ...
- time strptime()方法 时间操作
Python time strptime()方法 时间操作 描述 Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组. 语法 strptime()方法语 ...
- pandas之Seris和DataFrame
pandas是一个强大的python工具包,提供了大量处理数据的函数和方法,用于处理数据和分析数据. 使用pandas之前需要先安装pandas包,并通过import pandas as pd导入. ...
- state实例
States是SaltStack中的配置语言,在日常进行配置管理时需要编写大量的States文件. 比如我们需要安装一个包,然后管理一个配置文件,最后保证某个服务正常运行. 这里就需要我们编写一些st ...
- ⛅剑指 Offer 11. 旋转数组的最小数字
20207.22 LeetCode 剑指 Offer 11. 旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小 ...
- cpp求职
//Created by Arc on 2020/5/23 //////// Created by snnnow on 2020/5/20.//////面向对象的程序设计-期中测试// 根据题目实现求 ...
- Python异常及异常处理
Python异常及异常处理: 当程序运行时,发生的错误称为异常 例: 0 不能作为除数:ZeroDivisionError 变量未定义:NameError 不同类型进行相加:TypeError 异常处 ...