/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
System.Collections.Generic.Stack<TreeNode> S = new System.Collections.Generic.Stack<TreeNode>();
int maxDepth = ;
void postOrder(TreeNode Node)
{
if (Node != null)
{
S.Push(Node);
}
if (Node != null && Node.left != null)
{
postOrder(Node.left);
}
if (Node != null && Node.right != null)
{
postOrder(Node.right);
} var depth = S.Count; maxDepth = Math.Max(depth, maxDepth);
if (depth > )
{
S.Pop();
}
}
public int MaxDepth(TreeNode root) {
postOrder(root);
Console.WriteLine(maxDepth);
return maxDepth;
}
}

https://leetcode.com/problems/maximum-depth-of-binary-tree/#/description

补充一个python的实现:

 class Solution:
def maxDepth(self, root: 'TreeNode') -> 'int':
if root == None:
return
return max(self.maxDepth(root.left),self.maxDepth(root.right)) +

leetcode104的更多相关文章

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

  2. [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 ...

  3. leetCode104. 二叉树的最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], ...

  4. LeetCode104.二叉树最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], ...

  5. LeetCode-104.Maxinum Depth of Binary Tree

    Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longe ...

  6. leetcode104 Maximum Depth

    题意:二叉树最大深度 思路:递归,但是不知道怎么回事直接在return里面计算总是报超时,用俩变量就可以A···奇怪,没想通 代码: int maxDepth(TreeNode* root) { if ...

  7. leetcode-104.二叉树最大深度 · BTree + 递归

    easy 题就不详细叙述题面和样例了,见谅. 题面 统计二叉树的最大深度. 算法 递归搜索二叉树,返回左右子树的最大深度. 源码 class Solution { public: int maxDep ...

  8. leetcode104:permutations

    题目描述 给出一组数字,返回该组数字的所有排列 例如: [1,2,3]的所有排列如下 [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], [3,2,1].  (以数字在数 ...

  9. leetcode543

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

随机推荐

  1. 高级数据类型-- 字符串(str),切片

    一.字符串 字符串 就是 一串字符,是编程语言中表示文本的数据类型 在 Python 中可以使用 一对双引号" 或者 一对单引号' 定义一个字符串 虽然可以使用 \" 或者 \' ...

  2. 神州数码广域网Frame-Relay封装配置(即帧中继)

    实验要求:了解帧中继的配置方法 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 ip address ...

  3. Oracle 导出的表不全,以及数据库版本不同导入报错

    公司有两个环境下的数据库,版本不同,一个是11g r2,另一个是10g r2 首先在11g r2下用exp导出数据库备份文件,发现部分表缺失. 原来这部分表是空的,11G中新特性,当表无数据时,不分配 ...

  4. Java面试通关秘籍汇总集

    一.基础篇 1.1.Java基础 面向对象的特征:继承.封装和多态 final, finally, finalize 的区别 Exception.Error.运行时异常与一般异常有何异同 请写出5种常 ...

  5. IO流技术

    IO流常用基类 字节流的抽象基类:InputStream,OutputStream 字符流的抽象基类:Reader,Writer Writer类 子类:BufferedWriter,CharArray ...

  6. yum 的 group的信息

    查找 yum源中有哪些group及其详细信息 1:yum groupinfo '*' | less 2:yum groupinfo '*' | less +/sendmail-cf 将 sendmai ...

  7. java-Calendar类

    1.Calendar类的概述和获取日期的方法 * A:Calendar类的概述 * Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR ...

  8. vs+qt 运行过程出现cannot run rc.exe

    刚开始,我按照网上的一堆教程在qt creator中设置了各种东西,设置完以后,运行时出现cannot run rc.exe,根据百度,将C:\Program Files (x86)\Windows ...

  9. CSS样式补充第二天

    #p1{/*        border-width: 1px;*/        /*边框实线*/        /*border-style: solid;*/        /*边框虚线*/   ...

  10. asp.net服务器控件button先执行js再执行后台的方法

    服务器控件增强了在后台处理的能力,但是在前台有时候也被受到了限制. 关于button这个服务器控件,我一直想减少它向服务器提交数据.那些检测,还是在客户端实现就好了.这就需要javascript,但是 ...