Leetcode - Minimum Depth of Binary Tree

Question:

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.

My code:

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

Test result:

《Leetcode - Minimum Depth of Binary Tree》 Paste_Image.png

总结:
这道题目太简单了。。。
如果一定要找出什么总结的地方的话。我觉得就是如何使用递归吧。这里其实用到了一个函数重载 override 的概念。
题目给的方法是
public int minDepth(TreeNode root)

我自己重载了这个方法
private int minDepth(TreeNode root, int depth)

所以我在公共方法上的代码可以更加简单,而将递归过程写在了我的私有方法上。
希望女朋友四战托福可以上90!而我的目标是95!加油!
好久没刷题了,希望接下来可以坚持至少每天一道题目。

Anyway, Good luck, Richardo!

My code:

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

这道题木没有一遍过。想简单的改一下max depth of tree ,取最小值,但是不行。
因为如果一个结点的右结点是null的,那么他就不能算是有深度,不能直接返回0.
而要直接返回左侧的深度。

Anyway, Good luck, Richardo!

My code:

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

题目没能一遍过,还是想简单了。
在结点上不是去min + 1的。如果左孩子是空,那么返回来的0是不能考虑进去的。

然后写了下BFS版本:

My code:

/**
 * 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;
        }
        
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);
        int counter = 0;
        while (!q.isEmpty()) {
            int size = q.size();
            counter++;
            for (int i = 0; i < size; i++) {
                TreeNode curr = q.poll();
                if (curr.left == null && curr.right == null) {
                    return counter;
                }
                if (curr.left != null) {
                    q.offer(curr.left);
                }
                if (curr.right != null) {
                    q.offer(curr.right);
                }
            }
        }
        
        return counter;
    }
}

Anyway, Good luck, Richardo! — 09/06/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/dc4574636eee#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞