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

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;
        }
    }
}
时间: 2024-08-03 15:52:59

leetcode 117 Populating Next Right Pointers in Each Node II ----- java的相关文章

Java for LeetCode 117 Populating Next Right Pointers in Each Node II

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 t

LeetCode 117:Populating Next Right Pointers in Each Node II

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 t

LeetCode开心刷题五十五天——117. Populating Next Right Pointers in Each Node II

问题亟待解决: 1.一个问题一直困扰着我,想看下别人是怎么处理树的输入的,最好是以层级遍历这种清楚直观的方式. 2.关于指针*的使用 因此也导致代码不完整,没有主函数对Solution类的调用 117. Populating Next Right Pointers in Each Node II Medium 1161165FavoriteShare Given a binary tree struct Node { int val; Node *left; Node *right; Node

【57】117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Description Submission Solutions Add to List Total Accepted: 84339 Total Submissions: 251330 Difficulty: Medium Contributors: Admin Follow up for problem "Populating Next Right Pointers in Each Node

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

LeetCode[Tree]: Populating Next Right Pointers in Each Node II

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 t

【leetcode】Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II 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 s

117. Populating Next Right Pointers in Each Node II

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 tr

[leedcode 117] Populating Next Right Pointers in Each Node II

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 tr