题解 | #填充每个节点指向最右节点的next指针 ii#

填充每个节点指向最右节点的next指针 ii

http://www.nowcoder.com/practice/f18bc13a954f4389900b56e545feca6e

import java.util.*;

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root==null)
            return ;
        Queue<TreeLinkNode> q = new LinkedList<>();
        q.offer(root);

        while(!q.isEmpty()){
            int size = q.size();
            int t = 0;
            while(t < size){
                TreeLinkNode temp =  q.remove();
                if(temp.left!=null)    q.offer(temp.left);
                if(temp.right!=null)    q.offer(temp.right);
                if(t == size - 1)
                    temp.next = null;
                else
                    temp.next = q.peek();
                t++;
            }
        }
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务