LeetCode-Tree&DFS-116-M: 填充每个节点的下一个右侧节点指针(Populating Next Right Pointers in Each Node)


给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

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

你只能使用常量级额外空间。
使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

思路

(1)借用了之前的LeetCode-Tree-102-M:二叉树的层次遍历的两个队列的思想,但是不够优化。
(2)优化,借助size变量去掉一个队列。
(3)递归,常数级的空间复杂度。

解法1-两个队列

执行用时 :6 ms, 在所有 Java 提交中击败了5.57%的用户
内存消耗 :38.6 MB, 在所有 Java 提交中击败了44.20%的用户

class Solution {
    Queue<Node> queue1 = new LinkedList<>();
    Queue<Node> queue2 = new LinkedList<>();
    public Node connect(Node root) {
        // 层序遍历
        if(root == null){
            return null;
        }
        Node head = root;

        queue2.offer(root);

        while(!queue1.isEmpty() || !queue2.isEmpty()){
            while(queue1.isEmpty()){
                queue1 = new LinkedList<>(queue2);
                queue2.clear();
            }

            Node curNode=null;
            if(queue1.size()>1){
                curNode = queue1.poll();
                curNode.next = queue1.peek();
            }else{
                curNode = queue1.poll();
                curNode.next = null;
            }

            if(curNode.left != null){
                queue2.add(curNode.left);
            }
            if(curNode.right != null){
                queue2.add(curNode.right);
            }
        }

        return head;
    }
}

解法2-一个队列的BFS

(1)使用size变量解决了树的程序遍历中涉及的痛点问题。
空间复杂度:O(n)

执行用时 :3 ms, 在所有 Java 提交中击败了20.42%的用户
内存消耗 :39.9 MB, 在所有 Java 提交中击败了5.00%的用户

class Solution {
    public Node connect(Node root) {
        
        if(root == null){
            return null;
        }
              
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        
        while(!queue.isEmpty()){          
            int size =queue.size();           
            for(int i=0; i<size; i++){
                Node cur =queue.poll();
                if( i+1 ==size){
                    cur.next = null;
                }else{
                    cur.next = queue.peek();
                }
                
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }  
            }      
        }
        
        return root;
    }
}

解法3-递归

执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗 :39.6 MB, 在所有 Java 提交中击败了12.30%的用户

class Solution {
    public Node connect(Node root) {
        if(root == null){
            return null;
        }

        if(root.left != null){
            root.left.next = root.right;
            if(root.next != null){
                root.right.next = root.next.left;
            }
        }
        connect(root.left);
        connect(root.right);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值