{"id":133,"date":"2017-08-28T19:30:52","date_gmt":"2017-08-28T11:30:52","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=133"},"modified":"2018-11-27T16:31:40","modified_gmt":"2018-11-27T08:31:40","slug":"107-binary-tree-level-order-traversal-ii","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=133","title":{"rendered":"107. Binary Tree Level Order Traversal II"},"content":{"rendered":"<p>Given a binary tree, return the\u00a0<i>bottom-up level order<\/i>\u00a0traversal of its nodes&#8217; values. (ie, from left to right, level by level from leaf to root).<\/p>\n<p>For example:<br \/>\nGiven binary tree\u00a0<code>[3,9,20,null,null,15,7]<\/code>,<\/p>\n<pre>    3\r\n   \/ \\\r\n  9  20\r\n    \/  \\\r\n   15   7\r\n<\/pre>\n<p>return its bottom-up level order traversal as:<\/p>\n<pre>[\r\n  [15,7],\r\n  [9,20],\r\n  [3]\r\n]<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n# Definition for a binary tree node.\r\n# class TreeNode(object):\r\n#     def __init__(self, x):\r\n#         self.val = x\r\n#         self.left = None\r\n#         self.right = None\r\n\r\nclass Solution(object):\r\n    def __init__(self):\r\n        self.temp_result = dict()\r\n        self.result = list()\r\n        self.level = 0\r\n    def levelOrderBottom(self, root):\r\n        &quot;&quot;&quot;\r\n        :type root: TreeNode\r\n        :rtype: List[List[int]]\r\n        &quot;&quot;&quot;\r\n        if root:\r\n            self.return_node([root])\r\n            for i in range(0,len(self.temp_result)):\r\n                self.result.append(self.temp_result[i])\r\n\r\n            return self.result[::-1]\r\n\r\n        else:\r\n            return []\r\n\r\n\r\n    def return_node(self, root_list):\r\n        if root_list:\r\n            temp_list = []\r\n            for i in root_list:\r\n                temp_list.append(i.val)\r\n            self.temp_result[self.level] = temp_list\r\n            self.level += 1\r\n            new_root_list = list()\r\n            for i in root_list:\r\n                if i:\r\n                    if i.left:\r\n                        new_root_list.append(i.left)\r\n                    if i.right:\r\n                        new_root_list.append(i.right)\r\n            print(new_root_list)\r\n            return self.return_node(new_root_list)\r\n        return\r\n\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, return the\u00a0bottom-up level order\u00a0t<a href=\"https:\/\/www.ccagml.com\/?p=133\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">107. Binary Tree Level Order Traversal II<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/133"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}