`

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.

求一棵树的最短路径,同样用递归来解决。注意的是如果遇到一个节点只有一个孩子,这时我们要继续往下搜,要不然不符合题目的规定,同样的还有处理最大路径的问题,都是采用递归解决。代码如下:
最短路径:
/**
 * 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;
    }
}
0
0
分享到:
评论

相关推荐

    minimum-depth-of-binary-tree.rar_leetcode

    在提供的"minimum depth of binary tree"代码中,可能包含了上述的一种或多种解冑策略。解冑代码应该能够处理各种类型的二叉树,包括平衡树和高度不均衡的树,同时具有良好的时间复杂度和空间复杂度。具体实现细节和...

    java-leetcode-111-minimum-depth-of-binary-tree

    java java_leetcode-111-minimum-depth-of-binary-tree

    python-leetcode题解之111-Minimum-Depth-of-Binary-Tree

    python python_leetcode题解之111_Minimum_Depth_of_Binary_Tree

    js-leetcode题解之111-minimum-depth-of-binary-tree.js

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

    Leetcode book刷题必备

    27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个二叉树是否是平衡二叉树。 29. Convert Sorted Array to Balanced Binary Search Tree:将有序数组转换成平衡的二叉...

    LeetCode: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-...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-...

    leetcode下载-ARTS-:ARTS打卡

    Minimum Depth of Binary Tree 本题的要求是给一个二叉树,返回这个二叉树的最小层级 Tips Tmux 基本操作: prefix := Ctrl+B session相关操作 查看/切换session prefix s 离开Session prefix d 重命名当前Session ...

    LeetCode判断字符串是否循环-leetcode:用lin编码

    Minimum Depth of Binary Tree(111) 二叉树的遍历包括: 前序遍历 中序遍历 后序遍历 层次遍历 Points: 采用中序遍历实现 若左右子树均不为空,返回深度更小的一个;若其中某一子树为空,返回另一子树的深度 每个...

    maycope#Leetcode-Classic#1.1树的最小深度1

    1.1 树的最小深度Given a binary tree, find its minimum depth.The minimum depth is the n

    Leetcode题目+解析+思路+答案.pdf

    - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个...

    LeetCode最全代码

    * [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]...

    CleanCodeHandbook_v1.0.3

    6. BinaryTree(二叉树): 二叉树是每个节点最多有两个子节点的树数据结构,常用于组织数据,以便进行快速查找、插入和删除。文件中提到的二叉树相关题目包括: - Validate Binary Search Tree(验证二叉搜索树) ...

    最大公共字符串leetcode-leetcode:坚持每周刷一道LeetCode题

    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. 解题思路: 思路一:深度优先遍历,递归遍历左右子树...

    leetcode java

    - 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary Tree)问题需要检查一棵树是否是平衡的。 **位操作(Bit Manipulation)** 位操作是高级的编程技能,涉及...

    数据结构常用算法c++实现

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

    Computing and Combinatorics

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

Global site tag (gtag.js) - Google Analytics