• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
July 07, 2024 |250 Views

PROBLEM OF THE DAY : 06/07/2024 | Populate Inorder Successor for all Nodes

Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Yash Dwivedi. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Tree but also build up problem-solving skills.

Given a Binary Tree, complete the function to populate the next pointer for all nodes. The next pointer for every node should point to the Inorder successor of the node.
You do not have to return or print anything. Just make changes in the root node given to you.

Note: The node having no inorder successor will be pointed to -1. You don't have to add -1 explicitly, the driver code will take care of this.

Examples :

Input:       
              10       
              /  \      
            8   12      
          /    
        3
Output: 3->8 8->10 10->12 12->-1
Explanation: The inorder of the above tree is : 3 8 10 12. So the next pointer of node 3 is pointing to 8 , next pointer of 8 is pointing to 10 and so on.And next pointer of 12 is pointing to -1 as there is no inorder successor of 12.

Problem Link: https://practice.geeksforgeeks.org/problems/populate-inorder-successor-for-all-nodes/1
Solution Link: https://ide.geeksforgeeks.org/online-cpp14-compiler/dc0b5da4-24ac-4306-8ed2-c975b3716d16