Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: N-ary Tree Postorder Traversal

Posted on July 17, 2018July 26, 2020 by braindenny

N-ary Tree Postorder Traversal



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #treetraversal, #inspiring

Given an n-ary tree, return the postorder traversal of its nodes’ values.

For example, given a 3-ary tree:

Leetcode: N-ary Tree Postorder Traversal

Return its postorder traversal as: [5,6,3,2,4,1].

Note: Recursive solution is trivial, could you do it iteratively?

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution: recursive
## https://code.dennyzhang.com/n-ary-tree-postorder-traversal
## Basic Ideas:
## Complexity: Time O(n), Space O(n)
"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children
"""
class Solution(object):
    def postorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if root == None: return []
        res = []
        for node in root.children:
            res += self.postorder(node)
        res.append(root.val)
        return res
linkedin
github
slack

Post Views: 6
Posted in BasicTagged #inspiring, redo, treetraversal

Post navigation

LeetCode: Search in a Binary Search Tree
LeetCode: Longest Palindromic Subsequence

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.