【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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 TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

算法实现类

public class Solution {
    private int min = Integer.MAX_VALUE; // 记录树的最小深度
    private int cur = 0; // i当前处理的树的尝试

    public int minDepth(TreeNode root) {

        depth(root);
        return min;
    }

    /**
     * 计算树的深度
     *
     * @param node 当前结点
     */
    private void depth(TreeNode node) {

        if (node == null) {
            min = cur;
            return;
        }

        cur++; // 当前处理的层次加1
        // 如果是叶节点,并且路径比记录的最小还小
        if (node.left == null && node.right == null && cur < min) {
            min = cur; // 更新最小值
        }
        // 处理左子树
        if (node.left != null) {
            depth(node.left);
        }

        // 处理右子树
        if (node.right != null) {
            depth(node.right);
        }

        cur--; // 还原

    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47414057

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 16:48:19

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】的相关文章

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

[LeetCode]题解(python):111 Minimum Depth of Binary Tree

题目来源 https://leetcode.com/problems/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. 题意分析 Input: a binary tree

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 Difficulty: Easy 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 le

Java for LeetCode 111 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. 解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下: public int minDepth(TreeNode roo

[LeetCode] 111. Minimum Depth of Binary Tree Java

题目: 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. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

Java [Leetcode 111]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 { *

111. Minimum Depth of Binary Tree java solutions

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. Subscribe to see which companies asked this question Show Tags Show Similar Problems 1 /**

力扣算法题—111.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. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7

letecode [111] - 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. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7

【leetcode?python】 111. Minimum Depth of Binary Tree

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    depthList=[]    def minDepth(self, root):        ""&q