Skip to content

297. 二叉树的序列化与反序列化 #94

Description

@Geekhyt

原题链接

递归 dfs

const serialize = function(root) {
    const res = []
    function dfs(node) {
        if (node === null) {
            res.push(null)
            return
        }
        res.push(node.val)
        dfs(node.left)
        dfs(node.right)
    }
    dfs(root)
    return res
}
  • 时间复杂度: O(n)
  • 空间复杂度: O(n)
const deserialize = function(data) {
    function dfs() {
        if (data.length === 0) return null
        let val = data.shift()
        if (val === null) return null
        let node = new TreeNode(val)
        node.left = dfs()
        node.right = dfs()
        return node
    }
    return dfs()
}
  • 时间复杂度: O(n)
  • 空间复杂度: O(n)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions