给定一个 N 叉树,返回其节点值的 前序遍历 。
N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null
分隔(请参见示例)。
进阶:
递归法很简单,你可以使用迭代法完成此题吗?
示例 1:
输入:root = [1,null,3,2,4,null,5,6]
输出:[1,3,5,6,2,4]
示例 2:
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:[1,2,3,6,7,11,14,4,8,12,5,9,13,10]
提示:
- N 叉树的高度小于或等于
1000
- 节点总数在范围
[0, 10^4]
内
package Solution589;
import java.util.ArrayList;
import java.util.List;
class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> list = new ArrayList<>();
helper(root, list);
return list;
}
public void helper(Node root, List<Integer> list) {
if (root == null)
return;
list.add(root.val);
if (root.children != null) {
for (Node child : root.children) {
helper(child, list);
}
}
}
public static void main(String[] args) {
Solution sol = new Solution();
Node node5 = new Node(5);
node5.children = new ArrayList<>();
Node node6 = new Node(6);
node6.children = new ArrayList<>();
Node node3 = new Node(3);
node3.children = new ArrayList<>();
node3.children.add(node5);
node3.children.add(node6);
Node node2 = new Node(2);
node2.children = new ArrayList<>();
Node node4 = new Node(4);
node4.children = new ArrayList<>();
Node node1 = new Node(1);
node1.children = new ArrayList<>();
node1.children.add(node3);
node1.children.add(node2);
node1.children.add(node4);
System.out.println(sol.preorder(node1));
}
}
package Solution589b;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
class Solution {
public List<Integer> preorder(Node root) {
LinkedList<Integer> result = new LinkedList<>();
if (root == null) {
return result;
}
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node currentNode = stack.pop();
result.add(currentNode.val);
if (currentNode.children != null) {
Collections.reverse(currentNode.children); // were are reversing the children order in place
stack.addAll(currentNode.children);
}
}
return result;
}
public static void main(String[] args) {
Solution sol = new Solution();
Node node5 = new Node(5);
node5.children = new ArrayList<>();
Node node6 = new Node(6);
node6.children = new ArrayList<>();
Node node3 = new Node(3);
node3.children = new ArrayList<>();
node3.children.add(node5);
node3.children.add(node6);
Node node2 = new Node(2);
node2.children = new ArrayList<>();
Node node4 = new Node(4);
node4.children = new ArrayList<>();
Node node1 = new Node(1);
node1.children = new ArrayList<>();
node1.children.add(node3);
node1.children.add(node2);
node1.children.add(node4);
System.out.println(sol.preorder(node1));
}
}