/*
// 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 {
List<Integer> node = new LinkedList<>();
public List<Integer> postorder(Node root) {
result(root);
return node;
}
public void result(Node root) {
int index = 0;
//root等于空的时候,触底,向上返回
if (root == null) return;
//后序遍历,从左到右遍历儿子节点
while (index < root.children.size()) {
result(root.children.get(index));
index++;
}
node.add(root.val);
}
}
leetcode 590. N 叉树的后序遍历
最新推荐文章于 2022-03-12 10:41:42 发布