Java for Leetcode 117 populating Next right pointers in each Node II

Source: Internet
Author: User

Follow up to Problem "populating Next right pointersin 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

Problem solving idea One:

The difficulty of the subject is that it needs to be traversed by layer, so you can use Java for Leetcode 102 Binary Tree level Order traversal idea, Java implementation is as follows:

    public void Connect (Treelinknode root) {if (root = null | | (Root.left = = NULL && Root.right = = null))    Return        List<list<treelinknode>> list=new arraylist<list<treelinknode>> ();        queue<treelinknode> queue = new linkedlist<treelinknode> ();        Queue.add (root);            while (Queue.size ()! = 0) {list<treelinknode> alist = new arraylist<treelinknode> ();            for (Treelinknode child:queue) alist.add (child);            List.add (New arraylist<treelinknode> (alist));            Queue<treelinknode> Queue2=queue;            Queue=new linkedlist<treelinknode> ();                for (Treelinknode child:queue2) {if (child.left! = null) Queue.add (child.left);            if (child.right! = null) Queue.add (child.right);  }} for (List<treelinknode> alist:list) for (Treelinknode anode:alist)      Connectaroot (anode); public static void Connectaroot (Treelinknode root) {if (root = null | | (Root.left = = NULL && Root.right = = null)) Return;if (Root.next = = null) {if (root.left! = null) Root.left.next = Root.right;} else if (root.right = = null) {Treelinknode temp=root.next;while (temp!=null) {if (temp.left==null&&temp.right== NULL) Temp=temp.next;else break;} if (temp!=null) Root.left.next = (Temp.left = = Null?temp.right:temp.left);} else {if (root.left! = null) Root.left.next = Root.right; Treelinknode Temp=root.next;while (temp!=null) {if (temp.left==null&&temp.right==null) temp=temp.next;else break;}    if (temp!=null) Root.right.next = (Temp.left = = Null?temp.right:temp.left);} }

Two ways to solve problems:

Do not use the queue, please populating Next right pointers in each Node I [email protected]

Java for 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.