Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 297 · Construction / Serialization

Serialize and Deserialize Binary Tree

LeetCode 297 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Level-order with null markers; rebuild with a parent queue.

ಕಠಿಣStringTreeDepth-First SearchBreadth-First SearchDesign
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Serialize and Deserialize Binary Tree (LeetCode 297) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Construction / Serialization ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Write the tree in level order with '#' for missing children — that's enough information to rebuild it. When reading it back, a queue of parents receives its left and right children two tokens at a time.

Approach

  1. 1

    serialize: BFS, append value or '#', enqueue both children of real nodes.

  2. 2

    deserialize: first token is the root; queue it.

  3. 3

    For each parent from the queue, read the next two tokens as left and right.

  4. 4

    Real children are queued as future parents.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Level-order codecPythonO(n) time · O(n) space
class Codec:
    def serialize(self, root):
        if root is None:
            return ""
        encodedString = ""
        queue = [root]
        while queue:
            node = queue.pop(0)
            if node is None:
                encodedString += ",#" if encodedString else "#"
                continue
            encodedString += f",{node.val}" if encodedString else f"{node.val}"
            queue.append(node.left)
            queue.append(node.right)
        return encodedString

    def deserialize(self, data):
        if data == "":
            return None
        nodes = data.split(",")
        root = TreeNode(int(nodes[0]))
        queue = [root]
        i = 1
        while i < len(nodes):
            parent = queue.pop(0)
            if nodes[i] != "#":
                leftNode = TreeNode(int(nodes[i]))
                parent.left = leftNode
                queue.append(leftNode)
            i += 1
            if i < len(nodes) and nodes[i] != "#":
                rightNode = TreeNode(int(nodes[i]))
                parent.right = rightNode
                queue.append(rightNode)
            i += 1
        return root
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Serialize and Deserialize Binary Tree — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Serialize and Deserialize Binary Tree ಹಿಂದಿನ intuition ಏನು?

Write the tree in level order with '#' for missing children — that's enough information to rebuild it. When reading it back, a queue of parents receives its left and right children two tokens at a time.

Serialize and Deserialize Binary Tree (LeetCode 297) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. serialize: BFS, append value or '#', enqueue both children of real nodes. 2. deserialize: first token is the root; queue it. 3. For each parent from the queue, read the next two tokens as left and right. 4. Real children are queued as future parents.

Serialize and Deserialize Binary Tree ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n). Space: O(n). (Accepted approach: Level-order codec.)

Serialize and Deserialize Binary Tree ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Empty tree ↔ empty string. list.pop(0) is O(n); collections.deque makes each dequeue O(1) for big trees.