[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

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

For example, given a 3-ary tree:

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

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

Recursion Solution:

"""
# 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]
        """
        def dfs(r):
            if not r:
                return
            else:
                for c in r.children:
                    dfs(c)
                result.append(r.val)

        result=[]
        dfs(root)
        return result

  

Iteration Solution:

"""
# 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 not root:
            return []

        # We can use a deque to store the solution
        res=collections.deque()
        # We use stack to store all the node
        #Every time, we only need to pick the top node in the stack
        #and store its value in res
        #And then we store its children in the stack. The right-most
        #child is stored in the top.
        #If our stack is empty, we finish our job.
        stack=[root]

        while stack:
            u=stack.pop()
            res.appendleft(u.val)
            for c in u.children:
                stack.append(c)

        #change deque back to list
        return list(res)

  

原文地址:https://www.cnblogs.com/chiyeung/p/9728816.html

时间: 2024-11-08 00:00:41

[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal的相关文章

[LeetCode&Python] Problem 427. Construct Quad Tree

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it repres

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

590. N-ary Tree Postorder Traversal

1. Quesiton 590. N-ary Tree Postorder Traversal URL: https://leetcode.com/problems/n-ary-tree-postorder-traversal/description/ Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary tree: Return its posto

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Postorder Traversal [145]

[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? [题意] 非递归实现后续遍历 [思路] 维护两个栈,一个栈用来存储标记,标记相

LeetCode——Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 中文:二叉树的后续遍历(左-右-根).能用非递归吗? 递归: public class