zoukankan      html  css  js  c++  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.

    给一个二叉树,找出它的最小深度。最小深度是从根节点向下到最近的叶节点的最短路径,就是最短路径的节点个数。

    解法1:DFS

    解法2: BFS

    Java: DFS, Time Complexity: O(n), Space Complexity: O(n)

    /**
     * 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;
            }
            if (root.left == null) {
                return 1 + minDepth(root.right);
            } else if (root.right == null) {
                return 1 + minDepth(root.left);
            } else {
                return 1 + Math.min(minDepth(root.left), minDepth(root.right));    
            }
        }
    } 

    Java: BFS

    public class Solution {
        public int minDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            int curLevel = 1, nextLevel = 0;
            int depth = 1;
            
            while (!q.isEmpty()) {
                TreeNode node = q.poll();
                curLevel--;
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.left != null) {
                    q.offer(node.left);
                    nextLevel++;
                } 
                if (node.right != null) {
                    q.offer(node.right);
                    nextLevel++;
                } 
                if (curLevel == 0) {
                    curLevel = nextLevel;
                    nextLevel = 0;
                    depth++;
                }
            }
            return depth;
        }
    }  

    Python:

    class Solution(object):
        def minDepth(root):
            if root is None:
                return 0
    
            # Base Case : Leaf node.This acoounts for height = 1
            if root.left is None and root.right is None:
                return 1
    
            if root.left is None:
                return minDepth(root.right) + 1
    
            if root.right is None:
                return minDepth(root.left) + 1
    
            return min(minDepth(root.left), minDepth(root.right)) + 1  

    Python:

    class Solution:
        # @param root, a tree node
        # @return an integer
        def minDepth(self, root):
            if root is None:
                return 0
            
            if root.left and root.right:
                return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
            else:
                return max(self.minDepth(root.left), self.minDepth(root.right)) + 1  

    C++:

    /**
     * Definition for binary tree
     * 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;
            
            if (root->left == NULL) return minDepth(root->right) + 1;
            else if (root->right == NULL) return minDepth(root->left) + 1;
            else return 1 + min(minDepth(root->left), minDepth(root->right));
        }
        
    };
    

      

    类似题目:

    [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8674338.html
Copyright © 2011-2022 走看看