列表

详情


589. N 叉树的前序遍历

给定一个 n 叉树的根节点  root ,返回 其节点值的 前序遍历

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 叉树的层序遍历

N 叉树的后序遍历

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/* // Definition for a Node. class Node { public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; } }; */ class Solution { public: vector<int> preorder(Node* root) { } };

javascript 解法, 执行用时: 75 ms, 内存消耗: 53.5 MB, 提交时间: 2024-02-18 14:26:08

/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[]}
 */
var preorder = function(root) {
    const ans = [];
    function dfs(node) {
        if (node === null) {
            return;
        }
        ans.push(node.val);
        for (const c of node.children) {
            dfs(c);
        }
    }
    dfs(root);
    return ans;
};

cpp 解法, 执行用时: 10 ms, 内存消耗: 14.7 MB, 提交时间: 2024-02-18 14:25:50

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> preorder(Node *root) {
        vector<int> ans;
        function<void(Node*)> dfs = [&](Node *node) {
            if (node == nullptr) {
                return;
            }
            ans.push_back(node->val);
            for (auto c : node->children) {
                dfs(c);
            }
        };
        dfs(root);
        return ans;
    }
};

java 解法, 执行用时: 0 ms, 内存消耗: 43.8 MB, 提交时间: 2024-02-18 14:25:30

/*
// 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> preorder(Node root) {
        List<Integer> ans = new ArrayList<>();
        dfs(root, ans);
        return ans;
    }

    private void dfs(Node node, List<Integer> ans) {
        if (node == null) {
            return;
        }
        ans.add(node.val);
        for (Node c : node.children) {
            dfs(c, ans);
        }
    }
}

golang 解法, 执行用时: 4 ms, 内存消耗: 3.9 MB, 提交时间: 2024-02-18 14:24:58

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Children []*Node
 * }
 */

func preorder(root *Node) (ans []int) {
    var dfs func(*Node)
    dfs = func(node *Node) {
        if node == nil {
            return
        }
        ans = append(ans, node.Val)
        for _, c := range node.Children {
            dfs(c)
        }
    }
    dfs(root)
    return
}

python3 解法, 执行用时: 64 ms, 内存消耗: 16.8 MB, 提交时间: 2022-07-12 09:57:33

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        ans = []
        def dfs(node: 'Node'):
            if node is None:
                return
            ans.append(node.val)
            for ch in node.children:
                dfs(ch)
        dfs(root)
        return ans

python 解法, 执行用时: 176 ms, 内存消耗: N/A, 提交时间: 2018-08-24 21:39:09

"""
# 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 []
        if not root.children:
            return [root.val]
        result = [root.val]
        for child in root.children:
            result += self.preorder(child)
        return result

        
        

python 解法, 执行用时: 176 ms, 内存消耗: N/A, 提交时间: 2018-08-24 21:38:42

"""
# 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 [];
        if not root.children:
            return [root.val];
        result = [root.val];
        for child in root.children:
            result += self.preorder(child);
        return result;

        
        

上一题