LeetCode 589 N-ary Tree Preorder Traversal 解题报告

题目要求

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

题目分析及思路

题目给出一棵N叉树,要求返回结点值的前序遍历。可以使用递归的方法做。因为是前序遍历,所以最开始就加入根结点的值。

python代码

"""

# Definition for a Node.

class Node:

def __init__(self, val, children):

self.val = val

self.children = children

"""

class Solution:

def preorder(self, root: ‘Node‘) -> ‘List[int]‘:

order = []

if not root:

return order

order.append(root.val)

for child in root.children:

order.extend(self.preorder(child))

return order

原文地址:https://www.cnblogs.com/yao1996/p/10358663.html

时间: 2024-10-24 04:24:33

LeetCode 589 N-ary Tree Preorder Traversal 解题报告的相关文章

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

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

leetcode || 144、Binary Tree Preorder Traversal

problem: 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? Hide Tags Tree Stack 题意:非递归前序遍历二叉树 t

LeetCode OJ 144. 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? Subscribe to see which companies asked this qu

【LeetCode】144. 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: 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: Binary Tree Inorder Traversal 解题报告

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

LeetCode题解之N-ary Tree Preorder Traversal

1.题目描述 2.问题分析 采用递归方法是标准解法. 3.代码 1 vector<int> preorder(Node* root) { 2 vector<int> v; 3 preNorder(root, v); 4 return v; 5 } 6 7 void preNorder(Node *root , vector<int> &v) 8 { 9 if (root == NULL) 10 return ; 11 v.push_back(root->v