[Leetcode] 116. Populating Next right pointers on each Node solution ideas

Source: Internet
Author: User

Given a binary tree

    struct Treelinknode {      treelinknode *left;      Treelinknode *right;      Treelinknode *next;    }

Populate each of the next pointer to the next right node. If There is no next right node, the next pointer should are set to NULL .

Initially, all next pointers is set to NULL .

Note:

    • Constant extra space.
    • You could assume that it was a perfect binary tree (ie, all leaves was at the same level, and every parent had both children).

For example,
Given the following perfect binary tree,

         1       /        2    3     /\  /     4  5  6  7

After calling your function, the tree is should look like:

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

Problem: Given a binary tree, the *next of the tree element points to the horizontal right node of the element in the tree structure.

This is an application of breadth traversal. The group queue structure can be used to achieve breadth traversal and solve problems.

Ideas:

    • When the element in the queue is exactly the entire element of a row of trees, the *next of each node in the queue is assigned the corresponding next node in the list, in particular, the last element *next is NULL.
    • The elements in the queue are all ejected and then plugged into their child nodes, in which case the elements in the queue are exactly the entire elements of the next row of the tree, and proceed to the previous step.

The root node is plugged into the queue, which realizes the initialization of the above ideas.

1     voidConnect (Treelinknode *root) {2         3         if(Root = =NULL) {4             return;5         }6         7List<treelinknode*>queue;8         9 Queue.push_back (root);Ten          One          while(Queue.size () >0 ){ A              -             //Assign value to the next point of the the node in queue. -List<treelinknode*>:: iterator Q_iter; the              for(Q_iter = Queue.begin (); Std::next (Q_iter,1)! = Queue.end (); q_iter++){ -(*q_iter)->next = *std::next (Q_iter,1); -             } -              +             //Pop each node in the current row in the tree structure, and push the left and right childrens of them into queue. -              while(Queue.front ()->next! =NULL) { +treelinknode* node =Queue.front (); A Queue.pop_front (); at  -                 if(Node->left! =NULL) { -Queue.push_back (node->Left ); -                 } -                  -                 if(Node->right! =NULL) { inQueue.push_back (node->Right ); -                 } to             } +              -treelinknode* node =Queue.front (); the Queue.pop_front (); *              $             if(Node->left! =NULL) {Panax NotoginsengQueue.push_back (node->Left ); -             } the                  +             if(Node->right! =NULL) { AQueue.push_back (node->Right ); the             }       +         } -}

[Leetcode] 116. Populating Next right pointers on each Node solution ideas

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.