↓Skip to main content
  1. LeetCode/

LeetCode 589: N-ary Tree Preorder Traversal

·2 mins· ·
LeetCode Easy Stack Tree Depth-First-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-27

  • Submissions: Runtime Error → Accepted ×2 → Runtime Error ×2 → Wrong Answer ×4 → Runtime Error ×2 → Accepted → Runtime Error → Wrong Answer → Accepted ×5 → Runtime Error → Accepted ×4

Approach
#

Pre-order visits the node, then each child in order (Tree). Iteratively, pop and record, pushing children in reverse so the first child comes out first.

My Attempts
#

This took 24 submissions, mostly from the wrong shape of recursive results:

  • [root.val] + list(map(self.preorder, root.children)) inserted each child’s list as one element, giving the nested [1, [8]] instead of [1, 8].
  • Taking only the first element [0] of each child’s result dropped deeper nodes: [8, None, 3, None, 8] returned [8, 3] instead of [8, 3, 8].
  • After trying many ways to flatten, a loop with extend passed.

Solution
#

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        res, stack = [], [root] if root else []
        while stack:
            node = stack.pop()
            res.append(node.val)
            stack.extend(reversed(node.children))
        return res

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Recursively, flatten the children’s results: [root.val] followed by each child’s list.
  • Iteratively, push children onto the stack in reverse.

Interview Explanation
#

Pre-order is the node, then each child in order. With a stack I record on pop and push children in reverse so the first child is handled first. Time O(n).

Related

LeetCode 590: N-ary Tree Postorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search
LeetCode 590 note: post-order is each child then the node; iteratively, run a root-then-children-right-to-left pre-order and reverse the whole result
LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop
LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first
LeetCode 94: Binary Tree Inorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 94 note: iterative in-order traversal, pushing nodes while going left, recording on pop, then turning to the right subtree
LeetCode 559: Maximum Depth of N-ary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search
LeetCode 559 note: depth is 1 plus the deepest child, or 1 with no children
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer