LeetCode 590. N-ary Tree Postorder Traversal
Given the root
of an n-ary tree, return the postorder traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: [5,6,3,2,4,1]
Example 2:
Input: 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]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. 0 <= Node.val <= 104
- The height of the n-ary tree is less than or equal to
1000
.
Follow up: Recursive solution is trivial, could you do it iteratively?
递归,也是很简单,只是变成了先遍历children再加root.val。
-
/*
-
// Definition for a Node.
-
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> postorder(Node root) {
-
List<Integer> result = new ArrayList<>();
-
helper(root, result);
-
return result;
-
}
-
-
public void helper(Node root, List<Integer> result) {
-
if (root == null) {
-
return;
-
}
-
for (Node node : root.children) {
-
helper(node, result);
-
}
-
result.add(root.val);
-
}
-
}
迭代,还是记着Stack Set的方法呢,但是,debug了巨久都没搞出来,大概是seen set的地方弄错了。想着应该要把所有children都seen了才能pop它,但是刚开始想的是seen表示这个node有没有见过,于是要check所有children都seen了还挺麻烦,也不太确定这个思路对不对,就去看了答案。居然几乎没什么人跟我一个思路,看了一个相似的代码,才确认了确实是只有当node没有chidlren或者children全都seen过了才能pop,于是就把seen的含义变成了这个node的所有children是不是都seen过,调整了seen.add的位置,代码就很简洁也一下就没bug了。也算是加深了对postorder iterative的理解。
-
/*
-
// Definition for a Node.
-
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> postorder(Node root) {
-
List<Integer> result = new ArrayList<>();
-
if (root == null) {
-
return result;
-
}
-
Deque<Node> stack = new ArrayDeque<>();
-
Set<Node> seen = new HashSet<>(); // this node's children are all seen
-
stack.push(root);
-
while (!stack.isEmpty()) {
-
Node node = stack.peek();
-
int size = node.children.size();
-
if (size == 0 || seen.contains(node)) {
-
node = stack.pop();
-
result.add(node.val);
-
} else {
-
for (int i = size - 1; i >= 0; i--) {
-
Node child = node.children.get(i);
-
if (!seen.contains(child)) {
-
stack.push(child);
-
}
-
}
-
seen.add(node);
-
}
-
}
-
return result;
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhggiahc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13