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],

3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.

题目大意:

  给定二叉树,计算它从根节点到某个叶节点的最小路径(即节点数)。

理  解 :

  自己的思想是:从根节点开始,往下访问子节点,找到第一个叶节点,作为当前最小深度。后面遍历其他子树的时候,大于最小深度的子树就剪枝,小于就更新最小深度。实现的时候有点很奇怪的问题,暂时没调试出来。

  看了别人的思想。这个真的很棒。用队列辅助层次遍历,当访问的节点的左右子树均为空时,则当前节点为叶节点,返回当前层数即可。

代 码 C++:

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int minDepth(TreeNode* root) {        if(root==NULL) return 0;        if(root->left==NULL && root->right==NULL) return 1;        queue<TreeNode*> q;        q.push(root);        int size;        int level = 0;        while(!q.empty()){            size = q.size();            level++;            while(size>0){                TreeNode* node = q.front();                q.pop();                if(node->left==NULL && node->right==NULL)                    return level;                if(node->left!=NULL){                    q.push(node->left);                }                if(node->right!=NULL){                    q.push(node->right);                }                size--;            }        }        return level;            }};

运行结果:

  执行用时 : 20 ms  内存消耗 : 19.7 MB

原文地址:https://www.cnblogs.com/lpomeloz/p/10994124.html

时间: 2024-10-12 00:44:50

letecode [111] - Minimum Depth of 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

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

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 /**

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 { *

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]题解(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

力扣算法题—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

111. Minimum Depth of Binary Tree

和depth比较像 1 public int minDepth(TreeNode root) { 2 if(root == null) { 3 return 0; 4 } 5 if(root.left == null) { 6 return minDepth(root.right) + 1; 7 } 8 if(root.right == null) { 9 return minDepth(root.left) + 1; 10 } 11 return Math.min(minDepth(root.