Serialize a Binary Tree or a General Tree

For a binary tree, preorder traversal may be enough.

For example,

    _30_
   /    \   
  10    20
 /     /  \
50    45  35

The result is

30 10 50 # # # 20 45 # # 35 # #
Using a queue to deserialize it . 

But a for multi-way tree, we could also use an array to serialize it, e.g.,

30 10 20 50 45 35

-1   0    0   1    2   2

时间: 2024-11-05 13:50:36

Serialize a Binary Tree or a General Tree的相关文章

学习分享 lex tree diffrient row height(tree的行高调节)

Creating a Flex Tree with variable height rows can be mysteriously difficult if you are new to Flex item renderers.  This cookbook will give you the step by step instructions to creating a Tree control with variable height renderers. First you will n

102. Binary Tree Level Order Traversal (Tree, Queue; BFS)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \   15   7 return its level order traversal as: [  [3], 

[Leetcode] Binary search -- 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos

145. Binary Tree Postorder Traversal (Stack, Tree)

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? class Solution { public: vector<int> postor

124. Binary Tree Maximum Path Sum (Tree; DFS)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

[Leetcode] Binary tree-- 572. Subtree of Another Tree

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considere

199. Binary Tree Right Side View (Tree, Stack)

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1            <--- /   \2     3         <--- \     \  5     4 

【leetcode】1038. Binary Search Tree to Greater Sum Tree

题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a t

POJ 题目3237 Tree(Link Cut Tree边权变相反数,求两点最大值)

Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 6131   Accepted: 1682 Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with