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

Source: Internet
Author: User

"116-populating Next right pointers in each Node (binary tree link to the left hand)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question

Given a binary tree

    struct TreeLinkNode {      *left;      *right;      *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

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 a perfect binary 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

 Public classSolution { Public void Connect(Treelinknode Root) {Treelinknode node;//The input tree is assumed to be a completely forked tree        //The first loop is used to process the entire tree        //Root.left! = NULL if not, then processing to the last node with no left and right subtree will be an error         while(Root! =NULL&& Root.left! =NULL) {//Each root represents the first node of the first layer            //Node records the first node of each layernode = root;//process each layer             while(Node! =NULL) {//Indicates two sub-nodes that are connected to the same nodeNode.left.next = Node.right;//node is not the last node of a layer                if(Node.next! =NULL) {The right sub-node of this node is connected to the left Dial hand node of the adjacent node of the node.Node.right.next = Node.next.left; }//move to the next node in the same layernode = Node.next; }//move to the first node of the next layerroot = Root.left; }    }}
Evaluation Results

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

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47438089"

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

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

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.