leetcode111
/**
* 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 {
Stack<TreeNode> S = new Stack<TreeNode>(); List<List<TreeNode>> list = new List<List<TreeNode>>(); private void postNode(TreeNode node)
{
if (node != null)
{
S.Push(node);
if (node.left != null)
{
postNode(node.left);
}
if (node.right != null)
{
postNode(node.right);
} if (node.left == null && node.right == null)
{
list.Add(S.ToList());
}
S.Pop();
}
} public int MinDepth(TreeNode root)
{
postNode(root); var min = int.MaxValue; if (list.Count == )
{
min = ;
} foreach (var l in list)
{
var count = l.Count;
if (count < min)
{
min = count;
}
} return min;
}
}
https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description
补充一个python的实现:
class Solution:
def minDepth(self, root: TreeNode) -> int:
if root == None:
return
left = self.minDepth(root.left)
right = self.minDepth(root.right)
if left == or right == :
return left + right +
return min(left,right) +
leetcode111的更多相关文章
- [Swift]LeetCode111. 二叉树的最小深度 | Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode-111.Mininum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode111.二叉树的最小深度
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- LeetCode111. Minimum Depth of Binary Tree
题目 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15, ...
- leetcode-111. 二叉树最小深度 · Tree + 递归
题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...
- leetcode111:combination-sum
题目描述 给出一组候选数C和一个目标数T,找出候选数中加起来和等于T的所有组合. C中的数字在组合中可以被无限次使用 注意: 题目中所有的数字(包括目标数T)都是正整数 你给出的组合中的数字 (a 1 ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 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 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
随机推荐
- UVA-11761-马尔可夫/记忆化搜索
https://vjudge.net/problem/UVA-11762 给出一个整数n,每次随机挑选一个小于等于n的素数,如果是n的因子,n变为n/x ,否则不变,问n变为1的期望挑选次数. f[i ...
- HDU 5816 状压DP&排列组合
---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- web漏洞扫描工具集合
最好用的开源Web漏洞扫描工具梳理 链接:www.freebuf.com/articles/web/155209.html 赛门铁克2017年互联网安全威胁报告中提出在他们今年扫描的网站中,有76%都 ...
- python基础教程笔记 第1单元 && 第2单元
1.http://docs.python.org/dev/3.0/whatsnew/3.0.htmlpython-announce-listpython-help2.交互式编译器3.非整数除法 .1. ...
- 修改Intelij IDEA的maven依据下载为国内镜像(阿里)
1.win7环境,默认情况下在用户目录的.m2下自己新建setting文件.QQ群交流:697028234 .m2\settings.xml 2.settings.xml文件内容为: <sett ...
- Nvidia Driver
https://www.if-not-true-then-false.com/2015/fedora-nvidia-guide/ https://javacodes.cn/322.html https ...
- Hadoop1.1.2伪分布式安装
一.安装前准备设置Linux的静态IP修改VirtualBox的虚拟网卡地址修改主机名把hostname和ip绑定关闭防火墙:service iptables stop二.SSH免密码登陆生成秘钥文件 ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 10
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...
- PostgreSQL设置事务隔离级别实验
apple=# begin; BEGIN apple=# set transaction ISOLATION LEVEL read committed ; SET apple=# select * f ...
- Android EditText 中hint文字大小以及与输入文字颜色保存一致
SpannableString 这个就是用来处理android 文本信息 可编辑 可点击 感兴趣的自己去看! /* * Copyright (C) 2006 The Android Open Sour ...