1. 程式人生 > >演算法分析課每週練習 Serialize and Deserialize Binary Tree

演算法分析課每週練習 Serialize and Deserialize Binary Tree

題目

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

class Codec:

    def serialize(self, root):
        def doit(node):
            if node:
                vals.append(str(node.val))
                doit(node.left)
                doit(node.right)
            else:
                vals.append('#')
        vals = []
        doit(root)
        return ' '.join(vals)

    def deserialize(self, data):
        def doit():
            val = next(vals)
            if val == '#':
                return None
            node = TreeNode(int(val))
            node.left = doit()
            node.right = doit()
            return node
        vals = iter(data.split())
        return doit()

序列化二叉樹

相關推薦

演算法分析每週練習 Serialize and Deserialize Binary Tree

題目 Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or

演算法分析每週練習 Merge k Sorted Lists

題目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分析 Merge Sort 裡有合併Sorted List的步驟,

演算法分析每週練習 Binary Tree Maximum Path Sum

題目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting no

演算法分析每週練習 Word Search II

題目 Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters

演算法分析每週練習 The Skyline Problem

題目 有點長 分析 stackoverrflow 上有個關於這個問題的討論 class MaxHeap: def __init__(self, buildings): self.buildings = buildings self

演算法分析每週練習 Minimum Window Substring

題目 Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For e

演算法分析每週練習 Longest Increasing Path in a Matrix

題目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: l

演算法分析每週練習 Regular Expression Matching

題目 Implement regular expression matching with support for '.' and '*' 分析 參看前面的萬用字元匹配 class Solution: # @return a boolean def isM

演算法分析每週練習 Max Points on a Line

關於題目的選取 FIlter By Top Interview Questions依次選取 題目 Given n points on a 2D plane, find the maximum number of points that lie on the same s

演算法分析每週練習 First Missing Positive

題目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] retu

297. Serialize and Deserialize Binary Tree

leetcode serializa parent ant led elf while clas memory 題目: Serialization is the process of converting a data structure or object into a

LeetCode 297: Serialize and Deserialize Binary Tree

clas ini encode ria spa roo root tco span 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int va

難1 297. Serialize and Deserialize Binary Tree

ros yourself span there slist connect truct size com Serialization is the process of converting a data structure or object into a sequen

297. Serialize and Deserialize Binary Tree

tar efi bject += clas new ant tree code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *

Serialize and Deserialize Binary Tree

rest not sta state ppr des note proc binary Serialization is the process of converting a data structure or object into a sequence of bits

LeetCode - Serialize and Deserialize Binary Tree

解法一:recursion DFS 解法二:non-recursion BFS /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *

Leetcode:297. Serialize and Deserialize Binary Tree

題目: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memo

LC 297. Serialize and Deserialize Binary Tree

Serialize a tree,最容易的還是前序遍歷,用空格分開節點即可。 Deserialize 也很容易,遞迴即可。 /** * Definition for a binary tree node. * struct TreeNode { * int val; *

[Algorithm] 7. Serialize and Deserialize Binary Tree

markdown ras ive rip saving test ron fir esc Description Design an algorithm and write code to serialize and deserialize a binary tre

[LeetCode] 297. Serialize and Deserialize Binary Tree

Problem Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a f