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 postorder traversal as: [5,6,3,2,4,1].

2. Solution

# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children

class Solution(object):

    def postorderHelp(self,root,path):
        if root is None:
            return
        children = root.children
        for ch in children:
            self.postorderHelp(ch,path)
        path.append(root.val)

    def postorder(self, root):
        """
        :type
        root: Node
        :rtype: List[int]
        """
        re_li = []
        self.postorderHelp(root,re_li)
        return re_li

原文地址:https://www.cnblogs.com/ordili/p/9974930.html

时间: 2024-10-14 13:13:25

590. N-ary Tree Postorder Traversal的相关文章

【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

Leetcode590. N-ary Tree Postorder Traversal

590. N-ary Tree Postorder Traversal 自己没写出来 优秀代码: """ # 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

42: Binary Tree Postorder Traversal

/************************************************************************/            /*       42:  Binary Tree Postorder Traversal                               */            /************************************************************************/

LeetCode: Binary Tree Postorder Traversal 解题报告

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    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

[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][JavaScript]Binary Tree Postorder Traversal

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? 同样的配方,同样的味道.

【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进行操作,

Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以