337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3
    / \
   2   3
    \   \ 
     3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    / \
   4   5
  / \   \ 
 1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    map<TreeNode*, int> mt;
    map<TreeNode*, int> mf;
    
    inline int max(int a, int b){
        return a > b ? a : b;
    }
    inline int max4(int a, int b, int c, int d){
        int m = a;
        if(m < b) m = b;
        if(m < c) m = c;
        if(m < d) m = d;
        return m;
    }
    int fun(TreeNode* root, bool coin){
        if(NULL == root) return 0;
        
        if(coin){
            if(mt.count(root) > 0) return mt[root];
            int v = fun(root->left, false) + fun(root->right, false) + root->val;
            mt[root] = v;
            return v;
        }else{
            if(mf.count(root) > 0) return mf[root];
            
            int v = max4(
                fun(root->left, true) + fun(root->right, true), 
                fun(root->left, true) + fun(root->right, false),
                fun(root->left, false) + fun(root->right, true),
                fun(root->left, false) + fun(root->right, false)
            );
            mf[root] = v;
            return v;
        }
    }
    int rob(TreeNode* root) {
        if(NULL == root) return 0;
        return max(fun(root, true), fun(root, false));      
    }
};

Solution2:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    map<TreeNode*, int> mt;
    map<TreeNode*, int> mf;
    
    inline int max(int a, int b){
        return a > b ? a : b;
    }
    
    int dfs(TreeNode* root){
        if(NULL == root) return 0;
        if(mt.count(root) > 0) return mt[root];
        int val = 0;
        if(root->left) val += dfs(root->left->left) + dfs(root->left->right);
        if(root->right) val += dfs(root->right->left) + dfs(root->right->right);
        val = max(root->val + val, dfs(root->left) + dfs(root->right));
        mt[root] = val;
        return val;
    }
    
    int rob(TreeNode* root) {
        if(NULL == root) return 0;
        return dfs(root);    
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末悴侵,一起剝皮案震驚了整個(gè)濱河市采章,隨后出現(xiàn)的幾起案子轻纪,更是在濱河造成了極大的恐慌烹植,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窖贤,死亡現(xiàn)場(chǎng)離奇詭異砖顷,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)赃梧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門滤蝠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人授嘀,你說(shuō)我怎么就攤上這事物咳。” “怎么了蹄皱?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵览闰,是天一觀的道長(zhǎng)芯肤。 經(jīng)常有香客問我,道長(zhǎng)压鉴,這世上最難降的妖魔是什么纷妆? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮晴弃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘逊拍。我一直安慰自己上鞠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布芯丧。 她就那樣靜靜地躺著芍阎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缨恒。 梳的紋絲不亂的頭發(fā)上谴咸,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音骗露,去河邊找鬼岭佳。 笑死,一個(gè)胖子當(dāng)著我的面吹牛萧锉,可吹牛的內(nèi)容都是我干的珊随。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼柿隙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼叶洞!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起禀崖,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤衩辟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后波附,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體艺晴,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年掸屡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了财饥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡折晦,死狀恐怖钥星,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情满着,我是刑警寧澤谦炒,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布贯莺,位于F島的核電站,受9級(jí)特大地震影響宁改,放射性物質(zhì)發(fā)生泄漏缕探。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一还蹲、第九天 我趴在偏房一處隱蔽的房頂上張望爹耗。 院中可真熱鬧,春花似錦谜喊、人聲如沸潭兽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)山卦。三九已至,卻和暖如春诵次,著一層夾襖步出監(jiān)牢的瞬間账蓉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工逾一, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留铸本,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓遵堵,卻偏偏與公主長(zhǎng)得像归敬,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鄙早,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

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