[LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal

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

For example, given a 3-ary tree:

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

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

Recursion Method:

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children
"""
class Solution(object):
    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        l=[]

        def subpreorderfun(r):
            if r:
                l.append(r.val)
                for c in r.children:
                    subpreorderfun(c)

        subpreorderfun(root)

        return l

  

Iteration Method:

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children
"""
class Solution(object):
    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        l=[]
        q=[root]

        if root:
            p=[]
            while q:
                a=q.pop(0)
                l.append(a.val)

                for c in a.children:
                    p.append(c)

                n=len(p)
                for i in range(n):
                    q=[p.pop()]+q

        return l

  

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

时间: 2024-07-31 22:22:35

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

【Leetcode】【Medium】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]. 解题思路: 二叉树非递归先序遍历,使用栈来保存被遍历到的,但是还没遍历其右子树的结点. 代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode

【LeetCode从零单刷】Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解答: 这题难在使用迭代而不是递归.思考一下,先序遍历过程中是不是处理根之后,根的左子树变成新的根,还有子树

【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1, null, 1,2,3 ] 1 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 思路 二叉树的前序遍历方法分为递归法和使用循环辅助栈的方法,递归方法我们在递归左右节点之前先将当

[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 144]二叉树前序遍历Binary Tree Preorder Traversal

[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 2 / 3 Output: [1,2,3] [思路] 有参考,好机智,使用堆栈压入右子树,暂时存储. 左子树遍历完成后遍历右子树. [代码] /** * Definition for a binary tree node. * public class TreeNode { * in

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.

[LeetCode][JavaScript]Binary Tree Preorder 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? https://leetcod

[LeetCode 题解]: 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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>