Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to recover a binary tree from a traversal?
Forum Updated to NodeBB v4.3 + New Features

How to recover a binary tree from a traversal?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 158 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aliviya
    wrote on last edited by
    #1

    Re: How to recover a binary tree from a traversal?
    Here is a possible solution to recover the binary tree from the pre-order traversal and the child count information:

    • Create a stack to store the nodes that have not yet been processed.
    • Initialize the stack with the root node.
    • While the stack is not empty:
    • Pop the top node from the stack.
    • Get the child count of the node.
    • If the child count is 2:
    • Push the right child of the node onto the stack.
    • Push the left child of the node onto the stack.
    • If the child count is 1:
    • Push the child of the node onto the stack.
    • If the child count is 0:
    • Do nothing.
      This algorithm works by first pushing the root node onto the stack. Then, it repeatedly pops the top node from the stack and pushes its children onto the stack, one by one. The child count information is used to determine which children to push onto the stack.
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved