leetcode 117 Populating Next Right Pointers in Each Node II ----- java

來源:互聯網
上載者:User

標籤:ext   constant   node   roo   next   改變   while   ++   example   

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

 
這道題和上一道題的區別在於,上一道的樹是滿二叉樹,這一個並不是。

 

還是先使用隊列做了一次,ac但是速度並不是很快。

/** * 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) {        if( root == null )            return ;        Queue queue = new LinkedList<TreeLinkNode>();        queue.add(root);        while( !queue.isEmpty() ){            int size = queue.size();            TreeLinkNode node1 = (TreeLinkNode) queue.poll();            if( node1.left != null )                queue.add(node1.left);            if( node1.right != null)                queue.add(node1.right);            if( size == 1)                continue;            TreeLinkNode node2 = (TreeLinkNode) queue.poll();            if( node2.left != null )                queue.add(node2.left);            if( node2.right != null)                queue.add(node2.right);            for( int i = 2;i<size;i++){                node1.next = node2;                node1 = node2;                node2 = ( TreeLinkNode ) queue.poll();                if( node2.left != null )                    queue.add(node2.left);                if( node2.right != null)                    queue.add(node2.right);            }            node1.next = node2;        }            }}

 

但是題目中要求是常數空間。

所以還需要修改。

記錄上一行的開始和下一行的開始,然後依次改變next。

/** * 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) {                if( root == null )            return ;        TreeLinkNode low = null ;//指的是下面一行的第一個結點        TreeLinkNode up = root;        if( root.left != null )            low = root.left;        else if( root.right != null )            low = root.right;        while( low != null ){            TreeLinkNode start = low;            TreeLinkNode upStart = up;            helper(start,upStart);            while( low != null ){                if( low.left != null ){                    TreeLinkNode node = low.left;                    up = low;                    low = node;                    break;                }                if( low.right != null ){                    TreeLinkNode node = low.right;                    up = low;                    low = node;                    break;                }                low = low.next;            }        }    }    public void helper(TreeLinkNode start,TreeLinkNode upStart){        if( upStart.left != null){            if( upStart.right != null){                start.next = upStart.right;                start = start.next;            }        }        upStart = upStart.next;        while( upStart != null ){            if( upStart.left != null  ){                start.next = upStart.left;                start = start.next;                if( upStart.right != null ){                    start.next = upStart.right;                    start = start.next;                }            }else if( upStart.right != null ){                start.next = upStart.right;                start = start.next;            }            upStart = upStart.next;        }    }}

 

 

 

 

leetcode 117 Populating Next Right Pointers in Each Node II ----- java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.