题目地址:https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/
题目描述
Given an n-ary tree, return the preorder traversal of its nodes' values.
Forexample, 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?
题目大意
N叉树的前序遍历。
解题方法
递归
首先得明白,这个N叉树是什么样的数据结构定义的。val是节点的值,children是一个列表,这个列表保存了其所有节点。
前序遍历,即首先把根节点的值放到list中,然后再遍历其子节点们的值,同时对于每一个子节点也做同样的操作。
时间复杂度是O(N),空间复杂度是O(1).
Python代码如下:
"""
# 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]
"""
res = []
if not root:
return res
res.append(root.val)
for child in root.children:
res.extend(self.preorder(child))
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
C++代码如下:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
if (!root) return res;
res.push_back(root->val);
for (Node* child : root->children) {
vector<int> child_vector = preorder(child);
res.insert(res.end(), child_vector.begin(), child_vector.end());
}
return res;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
迭代
就像题目说的,递归方法很繁琐,能不能用迭代的方法去实现呢?答案很显然想到用栈!!
栈是一种后进先出的数据结构,先把root送入栈中,把它的数值放入结果中,然后需要把它的所有子节点逆序放入栈中。为什么是逆序?因为后进先出啊!我们需要在下一轮遍历它的最左孩子!
时间复杂度是O(N),空间复杂度是O(N).
Python代码如下:
"""
# 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]
"""
if not root: return []
stack = []
stack.append(root)
res = []
while stack:
node = stack.pop()
res.append(node.val)
stack.extend(node.children[::-1])
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
C++代码如下:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
if (!root) return res;
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* node = s.top(); s.pop();
if (!node) continue;
res.push_back(node->val);
for (int i = node->children.size() - 1; i >= 0; --i) {
s.push(node->children[i]);
}
}
return res;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发