- 浏览: 183432 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
求一棵树的最短路径,同样用递归来解决。注意的是如果遇到一个节点只有一个孩子,这时我们要继续往下搜,要不然不符合题目的规定,同样的还有处理最大路径的问题,都是采用递归解决。代码如下:
最短路径:
最长路径:
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
求一棵树的最短路径,同样用递归来解决。注意的是如果遇到一个节点只有一个孩子,这时我们要继续往下搜,要不然不符合题目的规定,同样的还有处理最大路径的问题,都是采用递归解决。代码如下:
最短路径:
/** * Definition for a binary tree node. * 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; return minDepth(root, 1); } public int minDepth(TreeNode root, int depth) { if(root == null) return 0; if(root.left != null && root.right != null) return Math.min(minDepth(root.left, depth + 1), minDepth(root.right, depth + 1)); if(root.left != null && root.right == null) return minDepth(root.left, depth + 1); if(root.left == null && root.right != null) return minDepth(root.right, depth + 1); return depth; } }
最长路径:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
在提供的"minimum depth of binary tree"代码中,可能包含了上述的一种或多种解冑策略。解冑代码应该能够处理各种类型的二叉树,包括平衡树和高度不均衡的树,同时具有良好的时间复杂度和空间复杂度。具体实现细节和...
java java_leetcode-111-minimum-depth-of-binary-tree
python python_leetcode题解之111_Minimum_Depth_of_Binary_Tree
javascript js_leetcode题解之111-minimum-depth-of-binary-tree.js
27. Minimum Depth of Binary Tree:二叉树的最小深度。 28. Balanced Binary Tree:判断二叉树是否为平衡二叉树。 29. Convert Sorted Array to Binary Search Tree:将有序数组转换为高度平衡的二叉搜索树。 30. ...
27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个二叉树是否是平衡二叉树。 29. Convert Sorted Array to Balanced Binary Search Tree:将有序数组转换成平衡的二叉...
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-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 本题的要求是给一个二叉树,返回这个二叉树的最小层级 Tips Tmux 基本操作: prefix := Ctrl+B session相关操作 查看/切换session prefix s 离开Session prefix d 重命名当前Session ...
Minimum Depth of Binary Tree(111) 二叉树的遍历包括: 前序遍历 中序遍历 后序遍历 层次遍历 Points: 采用中序遍历实现 若左右子树均不为空,返回深度更小的一个;若其中某一子树为空,返回另一子树的深度 每个...
1.1 树的最小深度Given a binary tree, find its minimum depth.The minimum depth is the n
- **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个...
* [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]...
6. BinaryTree(二叉树): 二叉树是每个节点最多有两个子节点的树数据结构,常用于组织数据,以便进行快速查找、插入和删除。文件中提到的二叉树相关题目包括: - Validate Binary Search Tree(验证二叉搜索树) ...
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. 解题思路: 思路一:深度优先遍历,递归遍历左右子树...
- 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary Tree)问题需要检查一棵树是否是平衡的。 **位操作(Bit Manipulation)** 位操作是高级的编程技能,涉及...
Prim's minimum spanning tree Kruskal MST Directed/Undirected graph ops Breadth First Search Depth First Search Dijkstra's algorithm Bellman-Ford algorithm Edmonds-Karp Maximal Flow Push–Relabel ...
Imbalance Is Fixed Parameter Tractable.- The Ramsey Number for a Linear Forest versus Two Identical Copies of Complete Graphs.- Computational Geometry.- Optimal Binary Space Partitions in the Plane.-...