LeetCode 589. N叉树的前序遍历

与后序方法类似,只需要更改list添加数的位置。

class Solution {

    List<Integer> list = new ArrayList();

    public List<Integer> preorder(Node root) {
        if (root != null) {
            list.add(root.val);
            for (Node node : root.children) {
                preorder(node);
            }
        }
        return list;
    }
}

猜你喜欢

转载自blog.csdn.net/u014239185/article/details/85320659