/**
* Minimum Depth of Binary Tree
*
* Given a binary tree, find its minimum depth.
*
* The minimum depth is the number of nodes along the shortest path from the
* root node down to the nearest leaf node.
*/
public class S111 {
public static void main(String[] args) {
}
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
int minLeft = minDepth(root.left);
int minRight = minDepth(root.right);
if(minLeft!=0 && minRight!=0){ // 还没到叶子节点,可以往左右继续走
return 1 + Math.min(minLeft, minRight);
}else if(minLeft != 0){ // 还没到叶子节点,可以往左继续走
return 1 + minLeft;
}else if(minRight != 0){ // 还没到叶子节点,可以往右继续走
return 1 + minRight;
}else{ // 到了叶子节点
return 1;
}
}
}
分享到:
相关推荐
leetcode 数据结构题目中的答案,已经调试,直接运行,求二叉树的最小深度
LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-...
Minimum Depth of Binary Tree Maximum Depth of Binary Tree Path Sum Path Sum II Binary Tree Maximum Path Sum Populating Next Right Pointers in Each Node Sum Root to Leaf Numbers LCA of Binary Tree 线段...
leetcode下载 ARTS- ARTS打卡 Algorithm Leetcode -111 Minimum Depth of Binary Tree 本题的要求是给一个二叉树,返回这个二叉树的最小层级 Tips Tmux 基本操作: prefix := Ctrl+B session相关操作 查看/切换...
1.1 树的最小深度Given a binary tree, find its minimum depth.The minimum depth is the n
LeetCode判断字符串是否循环 leetcode Coding with Lin on 1. Minimum Depth of Binary Tree(111) 二叉树的遍历包括: 前序遍历 中序遍历 后序遍历 层次遍历 Points: 采用中序遍历实现 若左右子树均不为空,返回...
最大公共字符串leetcode leetcode刷题打卡 接下来,坚持每周至少刷一道LeetCode题,刷题顺利按照牛客网LeetCode经典题目顺序进行,记录解题思路,与大家共享,同时也做个自我监督 第一周 1、树 题目: Given a ...
minimum_depth_binary_tree: twoSum: regularExpressionMathcing: sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,LeetCode 上...