mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 0 ms (100%), Space: 18.8 MB (17.56%) - LeetHub
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# 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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
|
||||
if root is None:
|
||||
return []
|
||||
|
||||
outNodes = [[root]]
|
||||
out = [[root.val]]
|
||||
nextHasChildren = root.left is not None or root.right is not None
|
||||
|
||||
while nextHasChildren:
|
||||
nextHasChildren = False
|
||||
nextLevel = []
|
||||
nextLevelNodes = []
|
||||
|
||||
for node in outNodes[-1]:
|
||||
if node.left is not None:
|
||||
nextLevel.append(node.left.val)
|
||||
nextLevelNodes.append(node.left)
|
||||
nextHasChildren |= node.left.left is not None or node.left.right is not None
|
||||
|
||||
if node.right is not None:
|
||||
nextLevel.append(node.right.val)
|
||||
nextLevelNodes.append(node.right)
|
||||
nextHasChildren |= node.right.left is not None or node.right.right is not None
|
||||
out.append(nextLevel)
|
||||
outNodes.append(nextLevelNodes)
|
||||
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user