Share EncyclopediaHome EncyclopediaCategories Switch Channel

0x094 middle sequence of the fork tree

2026-07-02 00:08710NameNetworking

94. The middle order of the fork tree

Https://letcode-cn. Com/problems/binary-tree-inorder-traversal/

Code

Ideas: using front pointer pre and root pointer through the whole fork tree

1 if x has no left child, add the value of x to the answer array before accessing the right child of x

2. If x has a left child, find the rightmost point on the left tree and mark it as a forward pre

I don't knowInordertreenode root
listAns = new arraylist();
freenode pre = full;
while (root! = full)
if (root. Left! = left)
/ / find the middle-order front of the current node
= root. Left;
while (pre. Right! = full & pre. Right! = root)
= pre. Right;
♪ i'm sorry ♪
if (pre. Right = null)
pre. Right = root; / pre. Right - > root, create middle sequence relay
root = root. Left; / / continue processing left trees
}else {/ / current node left tree is processed, current node is pressed directly and the trunk is broken
(c) ans. Add (root. Val);
= full;
root = root. Right; / / continue processing right subtrees
♪ i'm sorry ♪
}else {/ / no left subtree, current node directly presses
(c) ans. Add (root. Val);
root = root. Right; / / continue processing right subtrees
♪ i'm sorry ♪
♪ i'm sorry ♪
i'm sorry.
♪ i'm sorry ♪

Code

Ideas:

The way the fork tree goes through

Recursive exports: root = null

Ii. Middle-right history: center-left

ListAns = new arraylist();
i don't knowInordertreenode root
inorder (root);
i'm sorry.
♪ i'm sorry ♪
freenode root
if (root = null)
i'm sorry.
♪ i'm sorry ♪
inorder (root. Left);
(c) ans. Add (root. Val);
inorder (root. Right);
♪ i'm sorry ♪

Code

Ideas: the elements used to store the left tree

The way the fork tree goes through

First, the cur cycle moves left to the left boundary of the left tree, and the passing elements enter the indent

Two, and then one step back and the other do the same thing to the right tree

3. End of session only when there is no element or cur pointer empty subaru

I don't knowInordertreenode root
arraylistAns = new arraylist();
stackT = new stack();
freenode cur = root;
(cur! )
while (cur! = null){/ / left
st. Push (cur);
cur = cur. Left;
♪ i'm sorry ♪
cur = st. Pop();
;//c
cur = cur. Right; / / right
♪ i'm sorry ♪
i'm sorry.
♪ i'm sorry ♪

Code

Ideas:

The way the fork tree goes through

One, handle the left tree first, when cur. Left! = full, insert cur. Left into the inn and empty its left pointer

2. After processing the left tree, add the cur. Val to the result set and eject the top elements

Three, deal with the right subtree when cur. Right! = full, add cur. Right to the inn

4. The iterative exit: when the inn is empty, end the process

I don't knowInordertreenode root
arraylistAns = new arraylist();
stackT = new stack();
(c) st. Add (root);
while (! St. Isempty())
trenode cur = st. Peek();
if (cur = null)
st. Pop();
it's not a good idea.
♪ i'm sorry ♪
if (cur. Left! = full)
st. Add (cur. Left);
cur. Left = null;
it's not a good idea.
♪ i'm sorry ♪
;//c
st. Pop();
if (cur. Right! = full) {// right
st. Add (cur. Right);
♪ i'm sorry ♪
♪ i'm sorry ♪
i'm sorry.
♪ i'm sorry ♪
Like 0
Report
Favorite 0
Tip 0
Comment 0
Share 5
MoreRelated Comments
No comments yet, be the first to comment
I'll introduce you to the dancing
Welcome to the world of love in "drama of the day"! It's not a dream! The long-awaited couple's film has finally surfaced! O-ha-haI. Marriage system: the following figure can be found when single players click on the map of the wedding hall:When you click on friends, you can pop up a list of friends, you can choose a friend who wants a partner, you can launch an application with a partner, and when the application is successful, the friend receiv

0评论2026-08-0471