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 ...
随机推荐
- windows Jenkins git 配置
待更新 插件下载地址:http://updates.jenkins-ci.org/download/plugins/ 参考地址:https://blog.csdn.net/zzy1078689276/ ...
- 还是畅通工程,最小生成树kruskal
题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...
- 提供一个Java字符串转整型数组的方法
package edu.yuliang.Data_Structure_Basics; import java.util.Scanner; public class new_string { publi ...
- 王者荣耀交流协会 -- 第5次Scrum会议
Scrum master : 任思佳 要求1 : 工作照片 照片由王超同学拍摄 ,王露芝同学(外援)没有参加本次会议. 要求2 : 时间跨度:2017年10月17日 18:00 - 18:25 共计2 ...
- 100道Java基础面试题收集整理(附答案)
不积跬步无以至千里,这里会不断收集和更新Java基础相关的面试题,目前已收集100题. 1.什么是B/S架构?什么是C/S架构 B/S(Browser/Server),浏览器/服务器程序 C/S(Cl ...
- 第k大元素
在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注意 你可以交换数组 ...
- 冒泡排序到k趟
浙大pat题目 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们.通过一遍扫描,则最后一个元素必定是最大的元素.然后用同样的方 ...
- easyui datagrid去掉全选按钮
第一步: F12查看元素,选中全选按钮,把全选按钮的class里边加上display:none属性.找到对应的class,即.datagrid-header-check. 第二步: 在加载 表格的时候 ...
- leetcode 3.Longest Substring Without Repeating Charcters
在一个字符串中寻找出最长的无重复字符的子串的,在不断的后续检索中需要去掉前面一个重复的字符,那么就是需要记录之前所出现过的字符的,在这里需要利用hashmap来记录字符和其出现的位置之间的映射关系的, ...
- MySql之左连接,右连接
左连接,右连接查询的表 中 on后面的条件不会影响主表的数据,只会影响右表的数据. 例: 没加条件的时候 左表加条件: 右表加条件: 通过上面3处对比可以看出来,用LEFT JOIN 的时候不管对左表 ...