【leetcode】589. N-ary Tree Preorder Traversal

题目如下:

解题思路:凑数题+1,话说我这个也是凑数博?

代码如下:

class Solution(object):
    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if root == None:
            return []
        res = []
        stack = [root]
        while len(stack) > 0:
            node = stack.pop(0)
            res.append(node.val)
            for i in node.children[::-1]:
                stack.insert(0,i)
        return res

原文地址:https://www.cnblogs.com/seyjs/p/9397650.html

时间: 2024-08-30 15:49:40

【leetcode】589. N-ary Tree Preorder Traversal的相关文章

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

【LeetCode】Validate Binary Search Tree 解题报告

今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文. [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a

LeetCode之“树”: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? 递归解

【LeetCode】Recover Binary Search Tree 解题报告

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] 题意:二叉搜索树中,有两个结点的位置

【LeetCode】二叉查找树 binary search tree(共14题)

链接:https://leetcode.com/tag/binary-search-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [220]Contains Duplicate III [315]Count of Smaller Numbers After Self [327]Count of Range Sum [352]Data Stream as Disjoint Intervals [493]

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

【leetcode】1028. Recover a Tree From Preorder Traversal

题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  (If the depth of a node is D, the depth of its im

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归