mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 23:56:07 +00:00
Revisit the English version (#1885)
* Update giscus scroller. * Refine English docs and landing page * Sync the headings. * Update landing pages. * Update the avatar * Update Acknowledgements * Update landing pages. * Update contributors. * Update * Fix the formula formatting. * Fix the glossary. * Chapter 6. Hashing * Remove Chinese chars. * Fix headings. * Update giscus themes. * fallback to default giscus theme to solve 429 many requests error. * Add borders for callouts. * docs: sync character encoding translations * Update landing page media layout and i18n
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Array Representation of Binary Trees
|
||||
|
||||
Under the linked list representation, the storage unit of a binary tree is a node `TreeNode`, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees under the linked list representation.
|
||||
In the linked-list representation, the storage unit of a binary tree is a node `TreeNode`, and nodes are connected by pointers. The previous section introduced the basic operations of binary trees in this representation.
|
||||
|
||||
So, can we use an array to represent a binary tree? The answer is yes.
|
||||
|
||||
@@ -22,7 +22,7 @@ As shown in the figure below, given a non-perfect binary tree, the above method
|
||||
|
||||

|
||||
|
||||
To solve this problem, **we can consider explicitly writing out all `None` values in the level-order traversal sequence**. As shown in the figure below, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:
|
||||
To solve this problem, **we can explicitly write out all `None` values in the level-order traversal sequence**. As shown in the figure below, once we do this, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@@ -128,7 +128,7 @@ To solve this problem, **we can consider explicitly writing out all `None` value
|
||||
tree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
It's worth noting that **complete binary trees are very well-suited for array representation**. Recalling the definition of a complete binary tree, `None` only appears at the bottom level and towards the right, **meaning all `None` values must appear at the end of the level-order traversal sequence**.
|
||||
|
||||
@@ -136,9 +136,9 @@ This means that when using an array to represent a complete binary tree, it's po
|
||||
|
||||

|
||||
|
||||
The following code implements a binary tree based on array representation, including the following operations:
|
||||
The following code implements a binary tree using an array representation, including the following operations:
|
||||
|
||||
- Given a certain node, obtain its value, left (right) child node, and parent node.
|
||||
- Given a node, obtain its value, left (right) child node, and parent node.
|
||||
- Obtain the preorder, inorder, postorder, and level-order traversal sequences.
|
||||
|
||||
```src
|
||||
|
||||
Reference in New Issue
Block a user