[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 题目汇总

 

posted @ 2018-03-30 09:06  轻风舞动  阅读(532)  评论(0编辑  收藏  举报