117. Populating Next Right Pointers in Each Node II**

来源:互联网 发布:阿里云服务器1核2g 编辑:程序博客网 时间:2024/05/16 05:17

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1       /  \      2    3     / \    \    4   5    7

After calling your function, the tree should look like:

         1 -> NULL       /  \      2 -> 3 -> NULL     / \    \    4-> 5 -> 7 -> NULL
My code:

public class Solution {    public void connect(TreeLinkNode root) {        TreeLinkNode level_start = root;        while(level_start!=null){            TreeLinkNode cur = level_start;            while(cur!=null){                TreeLinkNode temp = cur.next;                while(temp!=null){                    if(temp.left!=null) {                        temp=temp.left;                        break;                    }                    else if(temp.right!=null) {                        temp=temp.right;                        break;                    }                    else temp=temp.next;                }                if(cur.right!=null){                    cur.right.next= temp;                    if(cur.left!=null){                        cur.left.next=cur.right;                    }                }                else{                    if(cur.left!=null)                        cur.left.next=temp;                }                cur=cur.next;            }            while(level_start!=null){                if(level_start.left!=null) {                    level_start=level_start.left;                    break;                }                else if(level_start.right!=null){                    level_start=level_start.right;                    break;                }                else level_start=level_start.next;            }        }    }}
总结:层次遍历

public class Solution {    public void connect(TreeLinkNode root) {        TreeLinkNode head = null; //head of the next level        TreeLinkNode prev = null; //the leading node on the next level        TreeLinkNode cur = root;  //current node of current level        while (cur != null) {                        while (cur != null) { //iterate on the current level                //left child                if (cur.left != null) {                    if (prev != null) {                        prev.next = cur.left;                    } else {                        head = cur.left;                    }                    prev = cur.left;                }                //right child                if (cur.right != null) {                    if (prev != null) {                        prev.next = cur.right;                    } else {                        head = cur.right;                    }                    prev = cur.right;                }                //move to next node                cur = cur.next;            }                        //move to next level            cur = head;            head = null;            prev = null;        }    }}
总结:省去了while循环,把对当前cur的搜索,改为对cur的child的搜索。




0 0