[leetcode]117 populating Next right pointers in each Node II

Source: Internet
Author: User

https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

http://blog.csdn.net/linhuanmars/article/details/23510601

/** * definition for binary tree with next pointer. * public  class TreeLinkNode { *     int val; *      TreeLinkNode left, right, next; *      Treelinknode (int x)  { val = x; } * } */public class  Solution {    public void connect (treelinknode root)  {                 // same as  https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/         // But queue is not allowed                 TreeLinkNode curhead = root;         treelinknode nexthead = null;         treelinknode pre = null; // pre in next level                 // iterate cur level,  assign next level        while  (curhead !=  null)         {             // Start to iterate current level             TreeLinkNode cur = curhead;             while  (cur != null)              {                 if  (cur.left != null)                  {                     if  (nexthead == null)                           nexthead = cur.left;                     if  (pre != null)                           pre.next = cur.left;                     pre = cur.left;                 }                                  if  (cur.right != null)                  {                     // same logic as leftnode handling.                      if  (nexthead == null)                          nexthead =  cur.right;                     if  (Pre != null)                          pre.next = cur.right;                     pre  = cur.right;                 }                 cur = cur.next;            }                          // After iteration this level.             // Move to next nevel.             if  (nexthead != null)              {                 curhead = nexthead;                 nexthead = null;                 pre = null;             }            else             {                 // No next head.                 // We are finished.                 curhead = null;             }        }     }}


[leetcode]117 populating Next right pointers in each Node 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.