<reponame>cnyy7/LeetCode_EY<filename>leetcode-algorithms/107. Binary Tree Level Order Traversal II/107.binary-tree-level-order-traversal-ii.py
#
# @lc app=leetcode id=107 lang=python3
#
# [107] Binary Tree Level Order Traversal II
#
# https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/
#
# algorithms
# Easy (48.66%)
# Likes:    1105
# Dislikes: 201
# Total Accepted:    289.3K
# Total Submissions: 572.4K
# Testcase Example:  '[3,9,20,null,null,15,7]'
#
# 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],
# 
# ⁠   3
# ⁠  / \
# ⁠ 9  20
# ⁠   /  \
# ⁠  15   7
# 
# 
# 
# return its bottom-up level order traversal as:
# 
# [
# ⁠ [15,7],
# ⁠ [9,20],
# ⁠ [3]
# ]
# 
# 
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        stack = deque([root])
        ans = collections.deque()
        while stack:
            one,length = [],len(stack)
            for _ in range(length):
                node = stack.popleft()
                one.append(node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
            ans.appendleft(one)
        return list(ans)
# @lc code=end


Binary Tree Level Order Traversal II/107.binary-tree-level-order-traversal-ii.py
#
# @lc app=leetcode id=107 lang=python3
#
# [107] Binary Tree Level Order Traversal II
#
# https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/
#
# algorithms
# Easy (48.66%)
# Likes:    1105
# Dislikes: 201
# Total Accepted:    289.3K
# Total Submissions: 572.4K
# Testcase Example:  '[3,9,20,null,null,15,7]'
#
# 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],
# 
# ⁠   3
# ⁠  / \
# ⁠ 9  20
# ⁠   /  \
# ⁠  15   7
# 
# 
# 
# return its bottom-up level order traversal as:
# 
# [
# ⁠ [15,7],
# ⁠ [9,20],
# ⁠ [3]
# ]
# 
# 
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        stack = deque([root])
        ans = collections.deque()
        while stack:
            one,length = [],len(stack)
            for _ in range(length):
                node = stack.popleft()
                one.append(node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
            ans.appendleft(one)
        return list(ans)
# @lc code=end
# # @lc app=leetcode id=107 lang=python3 # # [107] Binary Tree Level Order Traversal II # # https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/ # # algorithms # Easy (48.66%) # Likes:    1105 # Dislikes: 201 # Total Accepted:    289.3K # Total Submissions: 572.4K # Testcase Example:  '[3,9,20,null,null,15,7]' # # 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], # # ⁠   3 # ⁠  / \ # ⁠ 9  20 # ⁠   /  \ # ⁠  15   7 # # # # return its bottom-up level order traversal as: # # [ # ⁠ [15,7], # ⁠ [9,20], # ⁠ [3] # ] # # # # @lc code=start # Definition for a binary tree node. # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = None #         self.right = None # @lc code=end