Iterative approach for traversing a tree

Can someone help me with an algorithm to traverse a binary tree iteratively without using any other data structure like a stack

I read somewhere that we can have a flag with a name that was visited for each node, and turn on if the node is visited, but my BinaryTreeNode class does not have a specific visit variable. So I can not do something like node.left.visited = false

Is there any other way around the iteration?

+4
source share
1 answer

One option: thread binary tree.

, - node NULL ( ), , node node, ( , - ..). , .

:

enter image description here

, node node , . node node , , . , .

+7

Source: https://habr.com/ru/post/1534188/


All Articles