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 ...
随机推荐
- CSS3中的浮动
一.标准文档流:指元素根据块元素或行内元素的特性按从上到下,从左到右的方式自然排列.这也是元素默认的排列方式 二.display属性 display:更改块级元素和行内元素的相互转换 ...
- Echarts X轴 Y轴 线的类型、颜色、及标签字体颜色 设置
// 人均效能 var initRjxnChart = function () { var rjxnChart = echarts.init(document.getElementById(" ...
- 剑指Offer 55. 链表中环的入口结点 (链表)
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目地址 https://www.nowcoder.com/practice/253d2c59ec3e4bc68d ...
- centos安装VirtualBox增强包VBoxGuestAdditions
1.如果你的CentOS 版本早于 6,那么需要在 /etc/grub.conf 中添加一行 divider=10,以将这个参数传递给核心,以减少 idle CPU load. 2.#yum up ...
- tree-lstm初探
https://zhuanlan.zhihu.com/p/35252733 可以先看看上面知乎文章里面的例子 Socher 等人于2012和2013年分别提出了两种区分词或短语类型的模型,即SU-RN ...
- macbook 显示所有文件夹
在macbook终端执行如下代码: 1. 设置打开所有的文件 defaults write com.apple.finder AppleShowAllFiles -bool true 2. 关闭之前打 ...
- JavaEE思维导图
- Linux第一节课学习笔记
我的目标是考过RHCE 开源软件有使用.复制.传播.收费.修改及创建衍生品自由,其中后二者只有开源软件才有,前四者开闭源共有.
- zombodb 得分以及高光
得分以及高光在搜索引擎中有很重要的作用 得分zdb.score 使用方法 zdb.score(tid) 参考示例 SELECT zdb.score(ctid), * FROM products WHE ...
- Java自定义类加载器与双亲委派模型
其实,双亲委派模型并不复杂.自定义类加载器也不难!随便从网上搜一下就能搜出一大把结果,然后copy一下就能用.但是,如果每次想自定义类加载器就必须搜一遍别人的文章,然后复制,这样显然不行.可是自定义类 ...