Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Provide the Next Siblings Pointers in a Given Binary Tree

Objec­tive: - Given a binary tree with three point­ers left, right and nextSi­b­ling). Write the pro­gram to pro­vide the nextsi­b­ling pointers.

Exam­ple:

Provide-the-Next-Siblings-Pointers-in-a-Given-Binary-Tree.1

Provide-the-Next-Siblings-Pointers-in-a-Given-Binary-Tree.1

Approach:

  • Use Recur­sion.
  • Start from the root, if root’s left node is not null then make it point to the root’s right child.
  • check if root’s nextsi­b­ling is not null, if NOT then make the next sib­ling of root’s right node point to the root’s nextsibling’s left child. (In our exam­ple node 5 points node 6, as per our state­ment, Par­ent of node 5 is Node 2, next sib­ling of node 2 is node 3, and left child of node 2 is node 6, So Node 5 will points to Node 6 )

Code:


Out­put:

1
2 3
4 5 6 7

You may also like...