116. Populating Next Right Pointers in Each Node

题目描述

在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述Output: {“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","left":{"id”:“2”,“left”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“4”,“left”:null,“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“6”,“left”:null,“next”:null,“right”:null,“val”:7},“right”:null,“val”:6},“right”:null,“val”:5},“right”:null,“val”:4},“next”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"7","left":{"ref”:“5”},“next”:null,“right”:{“KaTeX parse error: Expected 'EOF', got '}' at position 9: ref":"6"}̲,"val":3},"righ…ref”:“4”},“val”:2},“next”:null,“right”:{"$ref":“7”},“val”:1}

Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.

题目链接

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

方法思路

Approach1 : level-traversal with Queue 这个方法基于所给二叉树是完美二叉树的基础。
空间复杂度好像不太符合题目要求 tricky!

class Solution {
    //Runtime: 2 ms, faster than 15.01%
    //Memory Usage: 35 MB, less than 93.85%
    public Node connect(Node root) {
        if(root == null) return root;
        Queue<Node> q1 = new LinkedList<>();
        Queue<Node> q2 = new LinkedList<>();
        double depth = 0.0;
        q1.offer(root);
        while(!q1.isEmpty()){
            q2.offer(q1.peek());
            Node node = q1.poll();
            if(node.left != null) q1.offer(node.left);
            if(node.right != null) q1.offer(node.right);
        }
        while(!q2.isEmpty()){
            Node node = q2.poll();
            double count = Math.pow(2.0, depth);
            while(count-- > 1.0){
                node.next = q2.peek();
                node = q2.poll();
            }
            node.next = null;
            depth++;
        }
        return root;
    }
}

Approach 2: traversal

class Solution {
    //Runtime: 0 ms, faster than 100.00%
    //Memory Usage: 35.7 MB, less than 81.92%
    public Node connect(Node root) {
        if(root == null) return root;
        root.next = null;
        Node level_start = root;
        while(level_start != null){
            Node cur = level_start;
            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;
            }
            level_start = level_start.left;
        }
        return root;
    }
}

Approach 3: recursive

class Solution {
    //Runtime: 0 ms, faster than 100.00%
    //Memory Usage: 36.1 MB, less than 71.97%
    public Node connect(Node root) {
        if(root == null)
            return root;
        
        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;
    }
}

Approach4:
同样是层序遍历,但效率要比方法一高上不少,这个方法同样可用于117. Populating Next Right Pointers in Each Node II

class Solution {
    //Runtime: 1 ms, faster than 48.14%
    //Memory Usage: 35.9 MB, less than 74.14%
    public Node connect(Node root) {
        if(root == null) return root;
        Queue<Node> q1 = new LinkedList<>();
        q1.offer(root);
        while(!q1.isEmpty()){
            int size = q1.size();
            Node node = q1.poll(), cur = node;
            while(size-- > 1){
                if(node.left != null) q1.offer(node.left);
                if(node.right != null) q1.offer(node.right);
                cur = q1.poll();
                node.next = cur;
                node = cur;
            }
            if(node.left != null) q1.offer(node.left);
            if(node.right != null) q1.offer(node.right);
        }            
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值