Traverse tree without recursion and stack in C

后端 未结 5 1216
日久生厌
日久生厌 2021-02-05 21:38

How to traverse each node of a tree efficiently without recursion in C (no C++)?

Suppose I have the following node structure of that tree:

struct Node
{
         


        
5条回答
  •  鱼传尺愫
    2021-02-05 22:22

    Generally you'll make use of a your own stack data structure which stores a list of nodes (or queue if you want a level order traversal).

    You start by pushing any given starting node onto the stack. Then you enter your main loop which continues until the stack is empty. After you pop each node from the stack you push on its next and child nodes if not empty.

提交回复
热议问题