107. Binary Tree Level Order Traversal II (Easy)

Description:

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],


return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]


Solution: 四種方案對比發(fā)現(xiàn)膝捞,value.insert(0,element)比value=[cache]+value要快!

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        
        value = []
        node_ls = [root]
        
        def helper(node_ls):
            cache = []
            for n in node_ls:
                if n.left != None:
                    cache.append(n.left)
                if n.right != None:
                    cache.append(n.right)
            return cache
        
        while node_ls:
            value.append([n.val for n in node_ls])
            node_ls = helper(node_ls)
            
        return value[::-1]

Runtime: 44 ms, faster than 58.17% of Python3 online submissions for Binary Tree Level Order Traversal II.
Memory Usage: 13.3 MB, less than 77.88% of Python3 online submissions for Binary Tree Level Order Traversal II.

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        
        value = []
        node_ls = [root]
        
        def helper(node_ls):
            cache = []
            for n in node_ls:
                if n.left != None:
                    cache.append(n.left)
                if n.right != None:
                    cache.append(n.right)
            return cache
        
        while node_ls:
            value.insert(0,[n.val for n in node_ls])
            node_ls = helper(node_ls)
            
        return value

Runtime: 32 ms, faster than 99.38% of Python3 online submissions for Binary Tree Level Order Traversal II.
Memory Usage: 13.4 MB, less than 72.12% of Python3 online submissions for Binary Tree Level Order Traversal II.

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        
        value = []
        node_ls = [root]
        last = root
        cache = []
        
        while node_ls:
            curr = node_ls.pop(0)
            cache.append(curr.val)
            
            if curr.left != None:
                node_ls.append(curr.left)
            if curr.right != None:
                node_ls.append(curr.right)

            if curr == last:
                value.insert(0,cache)
                cache = []
                if node_ls:
                    last = node_ls[-1]
            
        return value

Runtime: 40 ms, faster than 82.81% of Python3 online submissions for Binary Tree Level Order Traversal II.
Memory Usage: 13.6 MB, less than 25.46% of Python3 online submissions for Binary Tree Level Order Traversal II.

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if root == None:
            return []
        
        value = []
        node_ls = [root]
        last = root
        cache = []
        
        while node_ls:
            curr = node_ls.pop(0)
            cache.append(curr.val)
            
            if curr.left != None:
                node_ls.append(curr.left)
            if curr.right != None:
                node_ls.append(curr.right)

            if curr == last:
                value = [cache] + value
                cache = []
                if node_ls:
                    last = node_ls[-1]
            
        return value

Runtime: 44 ms, faster than 58.17% of Python3 online submissions for Binary Tree Level Order Traversal II.
Memory Usage: 13.7 MB, less than 22.73% of Python3 online submissions for Binary Tree Level Order Traversal II.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末傲武,一起剝皮案震驚了整個濱河市活喊,隨后出現(xiàn)的幾起案子余佃,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異豌研,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)唬党,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門鹃共,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驶拱,你說我怎么就攤上這事及汉。” “怎么了屯烦?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵坷随,是天一觀的道長。 經(jīng)常有香客問我驻龟,道長温眉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任翁狐,我火速辦了婚禮类溢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘露懒。我一直安慰自己闯冷,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布懈词。 她就那樣靜靜地躺著蛇耀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坎弯。 梳的紋絲不亂的頭發(fā)上纺涤,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機(jī)與錄音抠忘,去河邊找鬼撩炊。 笑死,一個胖子當(dāng)著我的面吹牛崎脉,可吹牛的內(nèi)容都是我干的拧咳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼囚灼,長吁一口氣:“原來是場噩夢啊……” “哼骆膝!你這毒婦竟也來了砾淌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谭网,失蹤者是張志新(化名)和其女友劉穎汪厨,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體愉择,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡劫乱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了锥涕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衷戈。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖层坠,靈堂內(nèi)的尸體忽然破棺而出殖妇,到底是詐尸還是另有隱情,我是刑警寧澤破花,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布谦趣,位于F島的核電站,受9級特大地震影響座每,放射性物質(zhì)發(fā)生泄漏前鹅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一峭梳、第九天 我趴在偏房一處隱蔽的房頂上張望舰绘。 院中可真熱鬧,春花似錦葱椭、人聲如沸捂寿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秦陋。三九已至,卻和暖如春掐松,著一層夾襖步出監(jiān)牢的瞬間踱侣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工大磺, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人探膊。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓杠愧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親逞壁。 傳聞我的和親對象是個殘疾皇子流济,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

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