"Leetcode-Interview algorithm classic-java Implementation" "" 117-populating Next right pointers in each Node (binary tree link to starboard pointer ii) "

Source: Internet
Author: User
Tags iterable

"117-populating Next right pointers in each Node (binary tree link to starboard pointer ii)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Follow up to problem "populating Next right pointers in each Node".
  What if the given tree could is any binary tree? Would your previous solution still work?
  Note:
Constant extra space.
For example,
Given The following binary tree,

         1       /        2    3     \        4   5    7

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

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

Main Topic

Given a binary tree, there is a next pointer that links each of their layers. Only constant extra space is used, and the tree is an arbitrary two- fork tree.

Thinking of solving problems

String Each node of the tree together with next. Each layer will also form a single linked list. And each layer of the linked list head, is, the root of the left child, left child, left child. Using a double loop, the outer loop, along the root of the left child, has been downward. The inner loop is responsible for stringing the nodes of the next layer together. That is, put your right child on the left child's next, and the right child, you can find the right neighbor through your next pointer.

Code Implementation

Tree Node class

publicclass TreeLinkNode {    TreeLinkNode left;    TreeLinkNode right;    TreeLinkNode next;}

Algorithm implementation class, using constant space

 Public classSolution { Publicvoid Connect (Treelinknode root) {Treelinknode queue = root; Treelinknode level =NewTreelinknode (0); while(Queue! =NULL) {level.Next=NULL; Treelinknode current = level; while(Queue! =NULL) {if(Queue. Left!=NULL) {Current.Next= Queue. Left; Current = current.Next; }if(Queue. Right!=NULL) {Current.Next= Queue. Right; Current = current.Next; } queue = queue.Next; } queue = level.Next; }    }}

Algorithm implementation class, using very much space

ImportJava.Util.Iterator;ImportJava.Util.LinkedList;ImportJava.Util.List; PublicClass Solution { Public voidConnect (Treelinknode root) {if(Root!= NULL) {//Save node            List<Treelinknode> List = NewLinkedList<>();//The previous node of the currently processed nodeTreelinknode prev= NULL;//node currently being processedTreelinknode node;///number of nodes remaining in the current layerint Curr= 1;//Record the number of elements in the next layerint Next= 0;//The root node is in the queue.            List.Add (root);//Queue non-empty             while(List.Size ()> 0) {//Delete team first elementNode= List.Remove0);///The number of current layer remaining is reducedCurr--;//Left dial hand tree non-empty, left Dial hand node queue                if(node.Left!= NULL) {List.Add (node.left); Next++; }//Right sub-tree non-empty, right sub-node queue                if(node.Right!= NULL) {List.Add (node.right); Next++; }//If the current layer is finished processing                if(Curr== 0) {//thread the elements on the next layerIterator<Treelinknode>Iterable= List.Iterator ();if(iterable.Hasnext ()) {prev=Iterable.Next (); while(iterable.Hasnext ()) {node=Iterable.Next (); Prev.Next=Node Prev=Node }                    }//Update the remaining node points in the current layerCurr=Next//re-count lower node pointsNext= 0; }            }        }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Use constant space

Use a very good amount of space

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47588657"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "" 117-populating Next right pointers in each Node (binary tree link to starboard pointer ii) "

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.