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

版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/84238288

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

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(别人的)

http://www.cnblogs.com/yrbbest/p/4437341.html

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

对每一行的节点,先处理它的下一行的next,(right的next与它的next的left和right有关),再递归它的左右子节点

/**
 * 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);
    }
}

猜你喜欢

转载自blog.csdn.net/xushiyu1996818/article/details/84238288