Member-only story

N-ary Tree Preorder Traversal — Leetcode 589

Java Solution for Leetcode 589

Suraj Mishra
3 min readNov 14, 2022

Introduction

  • In this article, we will solve Leetcode 589 problem which good problem to practice tree data structure
  • We will also look at stack based solution for this problem along with the recursive solution.

Problem Statement

  • We have been given N-ary tree and we need to return pre-order traversal of it’s node value.
  • Input tree represented has null value that is basically depicts the separation of children from parent.

Example

  • In this example , we can see input has null which means there is children for the given parent and its separated by null.
  • Pre order traversal means we first cover root node then left child and then right child, but since our tree is n-ary that means we will travel from left to right for the post order.
  • So pre order would be root ->left -> right children
  • Below example,[1,[[3,5,6],[2],[4]]]

Solution

--

--

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

No responses yet