问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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) {//若为空,返回 0if(root == null) return 0;//定义结果var res = 0;//定义一个栈,存一个键值对,键为节点,值为节点的深度var stack = new Stack<KeyValuePair<TreeNode, int>>();//把根节点压入栈中,其深度为 1stack.Push(new KeyValuePair<TreeNode, int>(root, 1));//处理栈,条件为栈不为空,即当栈空时终止while(stack.Any()) {//弹出栈顶元素所记录var top = stack.Pop();//每次比较最大值并存入 resres = Math.Max(res, top.Value);//为左、右子节点的深度计数if(top.Key.left != null) {//若左子节点不为空,则将左子节点压入栈中,并将其深度值 +1stack.Push(new KeyValuePair<TreeNode, int>(top.Key.left, top.Value + 1));}if(top.Key.right != null) {//若右子节点不为空,则将右子节点压入栈中,并将其深度值 +1stack.Push(new KeyValuePair<TreeNode, int>(top.Key.right, top.Value + 1));}}//返回 resreturn 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)相关推荐

  1. 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 104.求二叉树的最大深度 Maximum Depth of Binary Tree

    求二叉树的最大深度 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

  3. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  4. 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 ...

  5. 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 ...

  6. LeetCode - Maximum Depth of Binary Tree

    递归求二叉树的最大深度. /*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* Tre ...

  7. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  8. LC 104. Maximum Depth of Binary Tree

    1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...

  9. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

最新文章

  1. linux软中断的实现
  2. 两个input在一行让它们能对齐
  3. MetadataType的使用
  4. Chrome浏览器12px问题-webkit-text-size-adjust: none 已失效的解决方案
  5. 琥珀项目:较小的,面向生产力的Java语言功能
  6. fedora 忘记root密码
  7. emacs haskell mode 在windows 下的配置
  8. ReactJS入门学习一
  9. 提升内外网文件交换安全性,这里有5点建议
  10. html5页面签字,html5 canvas实现的手机端签字板
  11. 《路由器开发 - 路由器刷机指南》联想Newifi Y1刷机
  12. SAP 财务替代(基本内容及常用财务替代配置涉及退出提供源代码)
  13. P1428 小鱼比可爱
  14. dot格式绘图工具 html,使用dot来绘图
  15. 功放与喇叭的匹配原则
  16. cuda C++ cuFloatComplex/cufftComplex/复数 exp
  17. 「 LaTeX 」写论文,单双栏显示行号
  18. Excel分类汇总2个维度的结果,1个计数1个求和
  19. java工具封装树形对象,常用于菜单 json树逆向生成list集合
  20. 偏差代替误差进行稳态分析

热门文章

  1. Ethercat解析(五)之基础答疑
  2. 如何用Pygame写游戏(五)
  3. ubuntu 14.04.03 LTS(64bit) 安装PyCharm
  4. 《剑指Offer》用两个栈来实现队列
  5. 【今日CS 视觉论文速览】Fri, 21 Dec 2018
  6. 【Python管理GPU】pynvml工具的安装与使用
  7. mysql 中文乱码解决办法
  8. jdbc操作演示 mysql
  9. 线程名称的获取与修改
  10. 05-sqlyog的安装与基本使用