Leetcode - Balanced 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 boolean isBalanced(TreeNode root) {
        if (root == null)
            return true;
        return getDepth(root) == -1 ? false : true;
    }
    
    private int getDepth(TreeNode root) {
        if (root == null)
            return 0;
        int left = getDepth(root.left);
        if (left == -1)
            return -1;
        
        int right = getDepth(root.right);
        if (right == -1)
            return -1;
        
        if (Math.abs(left - right) < 2)
            return 1 + Math.max(left, right);
        else
            return -1;
        
    }
}

My test result:

Paste_Image.png

題目是easy級(jí)別,但可能,深夜,我太累了,腦子轉(zhuǎn)不過來,于是就沒寫出來。
本來不打算寫這道題目的,但還想拼一下,就寫了。
題目本身也是需要想想的。
具體看這個(gè)博客吧。
http://bangbingsyb.blogspot.com/2014/11/leetcode-balanced-binary-tree.html

我想說明的是,什么 depth of tree and height of tree
depth of node n: length of path from n to root.
所以說, depth of subtree 指的應(yīng)該就是 這棵subtree最大的深度,以該subtree結(jié)點(diǎn)為root。
所以, balanced binary tree的左右subtree的最大深度,不能超過1.
然后這個(gè)規(guī)則繼續(xù)遞歸給subtree的左右subtree。
然后需要bottom-up 來進(jìn)行判斷。具體看代碼吧。
還有就是,
height of node n: length of path from n to its deepest descendent.
= depth of the subtree.

**
總結(jié): Tree, DFS, Balance binary tree, depth of tree, height of tree
**

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 boolean isBalanced(TreeNode root) {
        if (root == null)
            return true;
        return helper(root) == -1 ? false : true;
    }
    
    /** if return -1, means it is not balanced */
    private int helper(TreeNode root) {
        if (root == null)
            return 0;
        int left = helper(root.left);
        if (left == -1)
            return -1;
        int right = helper(root.right);
        if (right == -1)
            return -1;
        if (Math.abs(left -right) <= 1)
            return Math.max(left, right) + 1;
        else 
            return -1;
    }
}

這道題目是從下往上的dfs

cf聊了那么多家,投了那么多家,竟然一家都沒給機(jī)會(huì)。。
不再畏懼了。沒什么準(zhǔn)備不準(zhǔn)備的。
海投,開始吧!
剛把a(bǔ)pple投了。

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 boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        
        int ret = helper(root);
        return ret == -1 ? false : true;
    }
    
    private int helper(TreeNode root) {
        if (root.left == null && root.right == null) {
            return 1;
        }
        else if (root.left == null) {
            int right = helper(root.right);
            if (right == -1 || right > 1) {
                return -1;
            }
            else {
                return 1 + right;
            }
        }
        else if (root.right == null) {
            int left = helper(root.left);
            if (left == -1 || left > 1) {
                return -1;
            }
            else {
                return 1 + left;
            }
        }
        else {
            int left = helper(root.left);
            int right = helper(root.right);
            if (left == -1 || right == -1) {
                return -1;
            }
            else if (Math.abs(left - right) > 1) {
                return -1;
            }
            else {
                return 1 + Math.max(left, right);
            }
        }
    }
}

差不多的思路。

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

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

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

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,927評(píng)論 0 33
  • 目錄 簡書的 markdown 都不支持 [TOC] 語法……我就不貼目錄了。下面按照類別,列出了29道關(guān)于二叉樹...
    被稱為L的男人閱讀 3,447評(píng)論 0 8
  • 我想, 正是因?yàn)榈厍蚴菆A的, 我們才活得如此圓滑。 我想, 正是因?yàn)樘鞖庾兓喽耍?我們才這般反復(fù)無常。 我想, ...
    乘風(fēng)乘云閱讀 175評(píng)論 0 1
  • 對(duì)于一個(gè)因懼怕跑操而每天祈禱極端天氣的小胖子來說,“負(fù)重跑對(duì)抗賽”恐怕更是噩夢(mèng)了。可是,比賽過后,小海同學(xué)幸...
    梁芳閱讀 731評(píng)論 0 4
  • 我一直認(rèn)為自己是一個(gè)堅(jiān)強(qiáng)而又獨(dú)立的姑娘,什么事情都喜歡自己來做。第一,自己做自己放心,第二,可以跟著自己想...
    顧錦妍閱讀 1,033評(píng)論 4 2

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