/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int minDepth(TreeNode root) { if(root==null){ return 0; } LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); LinkedList<TreeNode> childQueue = new LinkedList<TreeNode>(); queue.add(root); int depth = 1; while (!queue.isEmpty()){ TreeNode node = queue.remove(); if(node.left==null && node.right==null){ return depth; } if(node.left!=null){ childQueue.add(node.left); } if(node.right!=null){ childQueue.add(node.right); } if(queue.isEmpty() && !childQueue.isEmpty()){ depth++; queue.addAll(childQueue); childQueue.clear(); } } return depth; } }
相关推荐
public void helper(TreeNode root, int level){// 当前层没有 list,新建// 取得当前层的 list迭代pub
leetcode 数据结构题目中的答案,已经调试,直接运行,求二叉树的最小深度
LeetCode题解 - Java语言实现-181页.pdf
java面试 java面试_leetcode面试题解之最大数_java题解
leetcode 不会LeetCode_563--二叉树倾斜 给定一棵二叉树,返回整棵树的倾斜度。 树节点的倾斜度定义为所有左子树节点值的总和与所有右子树节点值的总和之间的绝对差。 空节点的倾斜度为 0。 整棵树的倾斜度定义为...
leetcode 2 二叉树打印机 在极小的区域打印二叉树。...>binary-tree-printer</ artifactId > < version >1.0.0</ version > </ dependency > 例子 打印随机 BST。 BTPrinter . printRandom
LeetCode 刷题笔记 with Java 51-100(暗黑版).pdf
答案leetcode-cn-java-algorithm-solution 我试图找到学习 leetcode 算法的最佳方法,所以我创建了它。 该项目将帮助您更好地学习 Leetcode 算法。 1. 入门 你想知道如何使用吗? 好的,现在让我们开始吧! 1.1 如何...
java笔试题-LeetCode 刷题笔记 with Java 1-50(暗黑版)
leetcode卡leetcode 二叉树卡片 LeetCode 二叉树卡片问题的章节智解
LeetCode 刷题笔记 with Java 1-50(暗黑版).pdf
leetcode题库所有数据库问题 Leetcode 所有数据库问题:Leetcode 问题 Active-Businesses-LeetCode.png 活跃用户-LeetCode.png 活动-参与者-LeetCode.png 至少合作过三次的演员和导演-LeetCode.png Ads-Performance-...
java lru leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 ...
Leetcode-Java- 一.(Sort类): 350. Intersection of Two Arrays II a.首先用HashMap遍历一遍数组nums1,Key值储存数组元素,Value(初始值为1)值储存重复元素出现次数,每出现一次加1; b.用List储存nums2中与nums...
leetcode 不会二叉树倾斜 给定一棵二叉树,返回整棵树的倾斜度。 树节点的倾斜度定义为所有左子树节点值的总和与所有右子树节点值的总和之间的绝对差。 空节点的倾斜度为 0。 整棵树的倾斜度定义为所有节点倾斜度的...
awesome-java-leetcode 我如今是一名 Android Developer,大学的我曾是一名 ACMer,我一直认为数据结构和算法是作为一名程序员必须掌握和善于利用的,为了不让数据结构和算法淡出我的记忆,所以我打算重拾 LeetCode ...
leetcode 答案LeetCode-答案-Java 我的 leetcode 用伪代码和分析用 java 回答
leetcode 530 LeetCode_530--BST 中的最小绝对差 给定具有非负值的二叉搜索树,找到任意两个节点值之间的最小绝对差。 例子: 笔记: 这个 BST 中至少有两个节点。 这道题和783一样:
leetcode添加元素使和等于 LeetCode 数组 Leetcode 0004 寻找两个正序数组的中位数 ----> ----> Leetcode 0027 移除元素 ----> ----> Leetcode 0041 缺失的第一个正数 ----> ----> Leetcode 0048 ...
leetcode求交集Binary-Search-4 问题1 两个数组的交集 II () 问题2 两个有序数组的中位数 ()