Skip to content

Latest commit

 

History

History
83 lines (64 loc) · 2.36 KB

_103. Binary Tree Zigzag Level Order Traversal.md

File metadata and controls

83 lines (64 loc) · 2.36 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 03, 2024

Last updated : July 03, 2024


Related Topics : Tree, Breadth-First Search, Binary Tree

Acceptance Rate : 60.2 %


Solutions

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root :
            return []
        toVisit = deque([root])
        leftRight = True
        output = []

        while toVisit :
            width = len(toVisit)
            lvl = []
            for i in range(width):
                if leftRight :
                    curr = toVisit.popleft()
                    lvl.append(curr.val)
                    if curr.left :
                        toVisit.append(curr.left)
                    if curr.right :
                        toVisit.append(curr.right)
                else :
                    curr = toVisit.pop()
                    lvl.append(curr.val)
                    if curr.right :
                        toVisit.appendleft(curr.right)
                    if curr.left :
                        toVisit.appendleft(curr.left)

            output.append(lvl)
            leftRight = not leftRight
        return output