/**
* 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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode111.二叉树的最小深度

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

  4. LeetCode111. Minimum Depth of Binary Tree

    题目 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15, ...

  5. leetcode-111. 二叉树最小深度 · Tree + 递归

    题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...

  6. leetcode111:combination-sum

    题目描述 给出一组候选数C和一个目标数T,找出候选数中加起来和等于T的所有组合. C中的数字在组合中可以被无限次使用 注意: 题目中所有的数字(包括目标数T)都是正整数 你给出的组合中的数字 (a 1 ...

  7. (二叉树 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 ...

  8. 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 ...

  9. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

随机推荐

  1. c#LINQ表达树

    如果你已经用过LINQ, 你应该知道函数方式,以及包含的丰富的类库, 如果你仍不了解, 那根据下面的链接去熟悉一下 the LINQ tutorial,  lambda. 表达树提供了丰富的包含参数的 ...

  2. keil软件相关问题汇总

    目录 一.keilc51——F12无法定位到定义处. 二.关于STM37F7的pack包不能安装报错SVD问题. 三.关于"Insufficient RAM for Flash Algori ...

  3. ISE创建Microblaze软核(一)

    在使用FPGA时,有时会用到它做为主控芯片.对于习惯于单片机及C语言开发的人,使用FPGA做主控芯片,首先还是想到它的嵌入式软核功能.如果能够基于Microblze软核进行C语言程序的开发,相对于使用 ...

  4. VScode+Flutter 开发继续踩坑

    运行慢解决方法1:修改build.gradle,注释掉jcenter(),google().使用阿里的镜像.原因是jcenter google库无法访问到导致的问题.虽然我有万能的爬墙工具,开启全局代 ...

  5. vue music 歌单组件

    在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...

  6. HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)

    Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...

  7. opencrud graphql 数据操作指南

    opencrud 是社区团队提出,同时prisma框架就是按照这个标准设计的,里面包含了对于graphql 数据 操作的最佳实践,目前还在完善中,但是设计以及指南覆盖的功能还是比较全的,如果用过 pr ...

  8. consul 1.2 支持service mesh

    主要说明: This release supports a major new feature called Connect that automatically turns any existing ...

  9. redis的maxmemory与maxmemory-policy关系

    如果redis配置了maxmemory和maxmemory-policy策略,则当redis内存数据达到maxmemory时,会根据maxmemory-policy配置来淘汰内存数据,以避免OOM.r ...

  10. CF 914G Sum the Fibonacci——子集卷积

    题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...