leetcode104
/**
* 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的更多相关文章
- 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 ...
- [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 ...
- leetCode104. 二叉树的最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], ...
- LeetCode104.二叉树最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], ...
- 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 ...
- leetcode104 Maximum Depth
题意:二叉树最大深度 思路:递归,但是不知道怎么回事直接在return里面计算总是报超时,用俩变量就可以A···奇怪,没想通 代码: int maxDepth(TreeNode* root) { if ...
- leetcode-104.二叉树最大深度 · BTree + 递归
easy 题就不详细叙述题面和样例了,见谅. 题面 统计二叉树的最大深度. 算法 递归搜索二叉树,返回左右子树的最大深度. 源码 class Solution { public: int maxDep ...
- leetcode104:permutations
题目描述 给出一组数字,返回该组数字的所有排列 例如: [1,2,3]的所有排列如下 [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], [3,2,1]. (以数字在数 ...
- leetcode543
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
随机推荐
- Vue - v-for 的延伸用法
1.v-for 合并标签template 一起使用 2.vue.set 1.v-for 合并标签template 一起使用 之前在设计table的时候,如果使用v-for ,会直接放在tr里面,一次产 ...
- mpvue 转小程序实践总结
介绍 Mpvue 是一个使用 Vue.js 开发小程序的前端框架. 基础介绍 框架基于 Vue.js 核心,修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序 ...
- php优秀框架codeigniter学习系列——CI_Router类学习
这篇文章主要介绍CI核心框架工具类CI_Router. 如果说CI_URI类是用来解析URI,那么CI_Router类就应该是根据解析出来的URI来决定究竟访问哪一个文件和哪一个function. 详 ...
- TestLink测试管理工具的使用说明
1. 创建项目: 主页左边的列表栏有 “Test Project management”的菜单,子菜单中有 “ create new test project”,通过它可以创建新的测试项目. 同时,菜 ...
- Python 判断是否可以转化为浮点数及其他数字类型
Python中出现ValueError: could not convert string to float:应该怎么处理 import sys import math # 判断是否为浮点数 def ...
- nodeJS文件操作
让前端觉得如获神器的不是NodeJS能做网络编程,而是NodeJS能够操作文件.小至文件查找,大至代码编译,几乎没有一个前端工具不操作文件.换个角度讲,几乎也只需要一些数据处理逻辑,再加上一些文件操作 ...
- CCF-权限查询-201612-3
这道题,开始只有10分.....原因是将false 写成了 flase 我要吐血而亡....关键是还debug了半天,以为是逻辑错了 不过亮点是代码很简洁,网上140+的代码看着真复杂 核心: 做题之 ...
- 作用域&&闭包
在了解闭包之前,先了解作用域一,作用域简单来说就是变量和函数可以访问的范围,在es5中变量作用域一般分为全局作用域和局部作用域,这个主要依据是全局变量还是局部变量 情景1: <script> ...
- MyBatis举例以及连接数据库过程
第一:创建实体类 package entity; public class Emp { private int empno; private String ename; private String ...
- Restful Service 中 DateTime 在 url 中传递
在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...