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:

Paste_Image.png

總結(jié):
這道題目太簡單了。。。
如果一定要找出什么總結(jié)的地方的話。我覺得就是如何使用遞歸吧。這里其實用到了一個函數(shù)重載 override 的概念。
題目給的方法是
public int minDepth(TreeNode root)

我自己重載了這個方法
private int minDepth(TreeNode root, int depth)

所以我在公共方法上的代碼可以更加簡單,而將遞歸過程寫在了我的私有方法上。
希望女朋友四戰(zhàn)托??梢陨?0!而我的目標(biāo)是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 ,取最小值,但是不行。
因為如果一個結(jié)點的右結(jié)點是null的,那么他就不能算是有深度,不能直接返回0.
而要直接返回左側(cè)的深度。

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;
        }
    }
}

題目沒能一遍過,還是想簡單了。
在結(jié)點上不是去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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容