力扣算法题之《117. 填充每个节点的下一个右侧节点指针 II》

[力扣算法题]《117. 填充每个节点的下一个右侧节点指针 II》

class Solution {
    public static Node connect(Node root) {
        // the second type: failed(It turns a little bit easy for me, but I still failed because I dont't quite understand with the usage of Queue(a Java abstract class), especially its instantiation.)
        if(root == null)return null;
        // 28 line error change to the following two lines
        Queue<Node> queue = new LinkedList<Node>();
        
        // Queue<Node> queue = new Queue<Node>(); // error 
        // error reason: Queue is the Abstract class, can not be instantiated,
        // should be instantiated by its subClass, name LinkedList, folling this:
        // Queue queue = new LinkedList();
        // Queue<Node> queue = new LinkedList<Node>(); // in this case
            
        Node f = null;
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size(); 
            for(int i = 1; i <= len; ++i){
                Node n = queue.poll();
                if(n.left != null){
                    queue.offer(n.left);
                }
                if(n.right != null){
                    queue.offer(n.right);
                }
                if(i != 1){
                    f.next = n;
                }
                f = n;
            }
        }
        return root;
    }  // the second type: failed
    

// the first type: failed(it's difficult for me!)
//     // following the official website
//     public Node connect(Node root) {
//         if(root == null)return null;
//         // Queue<Node> queuq = new LinkedList<Node>();
//         Queue<Node> queue = new LinkedList<Node>();
//         queue.offer(root);
//         while(!queue.isEmpty()){
//             int length = queue.size();
//             Node last = null;
//             // for(int i = 1; i < len; ++i){
//             // for(int i = 1; i <= lenght; ++i){ // error
//             for(int i = 1; i <= length; ++i){
//                 Node f = queue.poll();
//                 if(f.left != null){
//                     queue.offer(f.left);
//                 }
//                 if(f.right != null){
//                     queue.offer(f.right);
//                 }
//                 if(i != 1){
//                     // last = f;  // error
//                     last.next = f;
//                 }
                
//                 // if(i > 1){
//                 //     last.next = f;
//                 // }
//                 last = f;
//             }
//         }
//         return root;
//     }
//     // following the official website
    
    // error
    // public Node connect(Node root) {
    //     Node head = new Node(-1, null, root, null);
    //     wfs(head.right);
    //     return head.right;
    // }
    // public static void wfs(Node root){
    //     if(root == null || root.right == null){
    //         // root.next = null;
    //         return;
    //     } // 出口
    //     // while(root.right != null){
    //     // while(root.right == null){
    //     wfs(root.left);
    //     wfs(root.right);
    //     root.next = root.right;
    //     // }
    //     // wfs(root.left);
    //     // wfs(root.right);
    // }  // error
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值