Populating next right pointers in each node sets the next node of the Binary Tree

Source: Internet
Author: User

Given a binary tree

    struct TreeLinkNode {      TreeLinkNode *left;      TreeLinkNode *right;      TreeLinkNode *next;    }

 

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer shoshould be setNULL.

Initially, all next pointers are setNULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children ).

 

For example,
Given the following perfect binary tree,

         1       /        2    3     / \  /     4  5  6  7

 

After calling your function, the tree shoshould look like:

         1 -> NULL       /        2 -> 3 -> NULL     / \  /     4->5->6->7 -> NULL

Each node of a binary tree contains three pointer elements. Each node has an adjacent right node pointing to the same level in addition to the left and right subtree,

If there are no nodes on the right of the same level, set next to null. Otherwise, set next to the adjacent node on the right.

Solution:

Traverse each node at a time. If left and right subtree is not empty, next of the Left subtree is directed to the right subtree. If next of the node is empty, set next of the right subtree to null (View Graph Analysis)

If the next of the node is not empty, it points to the left subtree of the right node adjacent to the node in the same level as the node (see graph analysis)

Here, a queue is used to put the root node into the queue first, fetch a node from the queue, and remove the node from the queue. The next node of the node subtree is processed according to the above analysis.

The Code is as follows:

 1 /** 2  * Definition for binary tree with next pointer. 3  * struct TreeLinkNode { 4  *  int val; 5  *  TreeLinkNode *left, *right, *next; 6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7  * }; 8  */ 9 class Solution {10 public:11     void connect(TreeLinkNode *root) {12         if(root == NULL)    return;13         queue<TreeLinkNode *> q;14         15         root->next = NULL;16         17         q.push(root);18         19         while(!q.empty()){20             TreeLinkNode *node = q.front();21             q.pop();22             23             if(node->left != NULL && node->right != NULL){24                 q.push(node->left);25                 q.push(node->right);26                 27                 node->left->next = node->right;28                 if(node->next == NULL)29                     node->right->next = NULL;30                 else{31                     TreeLinkNode *node_next = q.front();32                     node->right->next = node_next->left;33                 }34                 35             }36             37         }38         39     }40 };

 

Populating next right pointers in each node sets the next node of the Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.