leetcode-116- 填充同一层的兄弟节点(populating next right pointers in each node)-java

题目及测试(测试不可用)

package pid116;


/*每个节点的右向指针

给定一个二叉树

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。

初始状态下,所有 next 指针都被设置为 NULL。

说明:

    你只能使用额外常数空间。
    使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
    你可以假设它是一个完美二叉树(即所有叶子节点都在同一层,每个父节点都有两个子节点)。

示例:

给定完美二叉树,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

调用你的函数后,该完美二叉树变为:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \  / \
4->5->6->7 -> NULL





}*/
public class main {
	
	public static void main(String[] args) {
		int[] ito=new int[]{3,9,20,15,7,4,8};
		int[] ito2=new int[]{9,3,15,20,7};
		Object[] x=new Object[]{3,9,20,15,7,4,8};	
		BinaryTree tree=new BinaryTree(x);
		tree.printTree(tree.root);
		test(tree.root);
		
		
	}
		 
	private static void test(TreeLinkNode ito) {
		Solution solution = new Solution();
		TreeLinkNode rtn;
		
		long begin = System.currentTimeMillis();
		solution.connect(ito);//执行程序
		long end = System.currentTimeMillis();		
		System.out.println("rtn=" );
		new BinaryTree(1).printTree(ito);
		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

解法1(成功,7ms,超慢)

利用层序遍历的思想,维护一个树节点队列,同时记录本层剩余的节点数和下一层的节点数。具体来说,初始先把根节点加入到队列中,每次从队列中取出一个节点:

首先判断它是否有左右子节点,若有则分别将其加入到队列中,并将下一层的节点数加一

然后从队列中弹出该节点,并将本层剩余节点数减一

若本层还有剩余节点,说明此节点有同一层的兄弟节点,所以将其next指针指向队首节点

若剩余节点数为0,说明遍历到本层的末尾,所以将剩余节点数置为下一层的节点数,并将下一层的节点数置为0

这样遍历直到队列为空,便可将树中所有next指针指向其同层兄弟节点。

 

package pid116;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/**
 * 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) {
    Queue<TreeLinkNode> queue=new LinkedBlockingQueue<>();
    int num=1;
    int next=0;
    TreeLinkNode now=null;
    TreeLinkNode prev=null;
    if(root==null){
    	return;
    }
    queue.add(root);
    while(!queue.isEmpty()){
    	now=queue.poll();
    	num--;
    	if(now.left!=null){
    		queue.add(now.left);
    		next++;
    	}
    	if(now.right!=null){
    		queue.add(now.right);
    		next++;
    	}
    	if(prev!=null){
    		prev.next=now;
    	}
    	prev=now;
    	if(num==0){
    		num=next;
    		next=0;
    		prev=null;
    	}
    }
    
    }
}

解法2(别人的)

递归,只要先判断next节点是否为空,接下来判定root的左右子节点是否为空就可以了。

即处理自己的儿子的next关系,然后让儿子处理孙子的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 p = root.next;
        if(root.left != null)
            root.left.next = root.right;
        if(root.right != null)
            root.right.next = (p == null) ? null : p.left;
        connect(root.left);
        connect(root.right);
    }
}

解法3(成功,0ms,极快)

即传来自己与自己的next,设置next关系,然后让儿子处理儿子与侄子的关系

    public Node connect(Node root) {
    	if(root!=null){
    		connectBrother(root, null);
    	}
    	return root;
    }	
	
	public void connectBrother(Node root,Node brother){
		root.next=brother;
		if(root.left!=null){
			connectBrother(root.left, root.right);
			if(brother==null){
				connectBrother(root.right, null);
			}else{
				connectBrother(root.right, brother.left);
			}
		}		
	}
	

解法4(别人的)

pre为每一行的第一个节点,cur从pre开始,不断到同一行的next,如果没有next了,到下一行的第一个

每次处理cur的儿子与侄子的关系

class Solution {
    public Node connect(Node root) {
        Node pre = root;
        while (pre != null) {
            Node cur = pre;
            while (cur != null) {
                if (cur.left != null) cur.left.next = cur.right;
                if (cur.right != null && cur.next != null) cur.right.next = cur.next.left;
                cur = cur.next;
            }
            pre = pre.left;
        }
        return root;
    }
}

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值