Leetcode - Maximum Depth of Binary Tree

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 maxDepth(TreeNode root) {
        return getDepth(root);
    }
    
    private int getDepth(TreeNode root) {
        if (root == null)
            return 0;
        int depth1 = getDepth(root.left);
        int depth2 = getDepth(root.right);
        return 1 + Math.max(depth1, depth2);
    }
}

My test result:

Paste_Image.png

簡單題。。。為了完成今天5道題的目標,但是又刷不動了,選了這道簡單題。
沒什么好分析的。

**
總結(jié): DFS, bottom-up, post-order
**

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 maxDepth(TreeNode root) {
        return root == null ? 0 : 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}

一行解決問題。
fuck. 也就只能在簡單題里面裝個逼了

Anyway, Good luck, Richardo!

DFS recursion
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 maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return 1 + Math.max(left, right);
    }
}

pre-order

DFS iteration
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 maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Stack<TreeNode> st = new Stack<TreeNode>();
        Stack<Integer> value = new Stack<Integer>();
        st.push(root);
        value.push(1);
        int max = 0;
        while (!st.isEmpty()) {
            TreeNode node = st.pop();
            int temp = value.pop();
            max = Math.max(max, temp);
            if (node.left != null) {
                st.push(node.left);
                value.push(temp + 1);
            }
            if (node.right != null) {
                st.push(node.right);
                value.push(temp + 1);
            }
        }
        
        return max;
    }
}

用兩個棧的方法很巧妙。同時暫存頭結(jié)點的depth -> temp

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

reference:
https://discuss.leetcode.com/topic/33826/two-java-iterative-solution-dfs-and-bfs

得把 tree 的三種遍歷方式 iteration, recursion 都復(fù)習(xí)一遍。

Anyway, Good luck, Richardo! 08/28/2016

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容