156. Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

For example:
Given a binary tree {1,2,3,4,5},

    1
   / \
  2   3
 / \
4   5

return the root of the binary tree [4,5,2,#,#,3,1].

   4
  / \
 5   2
    / \
   3   1  

一刷
題解:

方法1: recursive


recursive

newRoot存在的意義是keep住最left的leaf

public TreeNode upsideDownBinaryTree(TreeNode root) {
    if(root == null || root.left == null) {
        return root;
    }
    
    TreeNode newRoot = upsideDownBinaryTree(root.left);
    root.left.left = root.right;   // node 2 left children
    root.left.right = root;         // node 2 right children
    root.left = null;
    root.right = null;
    return newRoot;
}

方法2: iterative
注意日戈,temp在上一輪中就要保存好,避免被overwritten.


iterative
public TreeNode upsideDownBinaryTree(TreeNode root) {
    TreeNode curr = root;
    TreeNode next = null;
    TreeNode temp = null;
    TreeNode prev = null;
    
    while(curr != null) {
        next = curr.left;
        
        // swapping nodes now, need temp to keep the previous right child
        curr.left = temp;
        temp = curr.right;
        curr.right = prev;
        
        prev = curr;
        curr = next;
    }
    return prev;
}  

二刷
遍歷順序?yàn)椋簂eft-root

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if(root == null || root.left == null) return root;
        
        TreeNode newRoot = upsideDownBinaryTree(root.left);
        root.left.left = root.right;
        root.left.right = root;
        root.left = null;
        root.right = null;
        return newRoot;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蹋嵌,一起剝皮案震驚了整個(gè)濱河市未玻,隨后出現(xiàn)的幾起案子番甩,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妈拌,死亡現(xiàn)場離奇詭異考润,居然都是意外死亡狭园,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門糊治,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唱矛,“玉大人,你說我怎么就攤上這事井辜∫锴” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵粥脚,是天一觀的道長窃肠。 經(jīng)常有香客問我,道長阿逃,這世上最難降的妖魔是什么铭拧? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮恃锉,結(jié)果婚禮上搀菩,老公的妹妹穿的比我還像新娘。我一直安慰自己破托,他們只是感情好肪跋,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著土砂,像睡著了一般州既。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上萝映,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天吴叶,我揣著相機(jī)與錄音,去河邊找鬼序臂。 笑死蚌卤,一個(gè)胖子當(dāng)著我的面吹牛实束,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逊彭,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼咸灿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了侮叮?” 一聲冷哼從身側(cè)響起避矢,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎囊榜,沒想到半個(gè)月后审胸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锦聊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年歹嘹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片孔庭。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡尺上,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出圆到,到底是詐尸還是另有隱情怎抛,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布芽淡,位于F島的核電站马绝,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏挣菲。R本人自食惡果不足惜富稻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望白胀。 院中可真熱鬧椭赋,春花似錦、人聲如沸或杠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽向抢。三九已至认境,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挟鸠,已是汗流浹背叉信。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留艘希,地道東北人茉盏。 一個(gè)月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓鉴未,卻偏偏與公主長得像枢冤,于是被迫代替她去往敵國和親鸠姨。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359

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