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 ...
随机推荐
- JAVA集合接口及类
各接口及类关系图 Iterable 所有集合的初始接口,实现该接口可进行foreach操作,只有一个iterator()方法,并返回iterator类型: Iterable在java.lang下,It ...
- Python基础04_str_方法
所有的练习都是用的python3 ,还没试过python2 ############## 必须要会的7个基本方法 ############## join split find strip upper ...
- FCC JS基础算法题(10):Falsy Bouncer(过滤数组假值)
题目描述: 删除数组中的所有假值.在JavaScript中,假值有false.null.0."".undefined 和 NaN. 使用filter方法,过滤掉生成的 Boolea ...
- React Native 安卓 程序运行报错: React Native version mismatch(转载)
这个问题已经得到解决,参照stackoverflow上的问题:https://stackoverflow.com/que...这个问题的原因就处在Android工程中app/build.gradle中 ...
- 【linux基础】关于ARM板子使用O3编译选项优化
前言 应领导要求需要将最初级版本的算法移植到ARM板子上,并进行优化,以期达到实时. 平台 移植前: TX2 移植后: ARM() processor : model name : ARMv7 Pro ...
- WEBBASE篇: 第四篇, CSS知识2
CSS知识2 一, 尺寸 与 边框 CSS单位 1,尺寸单位:(1)px 像素 (2)% (3) in 英寸 lin = 2.54cm (4)pt 磅 1pt = 1/72in ppi ...
- centos安装VirtualBox增强包VBoxGuestAdditions
1.如果你的CentOS 版本早于 6,那么需要在 /etc/grub.conf 中添加一行 divider=10,以将这个参数传递给核心,以减少 idle CPU load. 2.#yum up ...
- 【数据库(一)】SQL语言-表定义、查询
基本模式定义+ SQL支持许多不同的完整性约束. not null, 在该属性上不允许空值 primary key 是否是是主码,主码必须非空且唯一 foreign key check(P),P是谓词 ...
- ViewpagerHandler
import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.su ...
- Ubuntu16.04中pip无法更新升级,采用源码方式安装
1.从pip官网下载最新版 https://pypi.org/project/pip/#files 2.ubuntu中创建文件位置,我的放在一下路径,之后进行解压 3.解压后进入pip的文件夹,在执行 ...