|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=116 lang=java |
| 3 | + * |
| 4 | + * [116] Populating Next Right Pointers in Each Node |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (39.05%) |
| 10 | + * Likes: 1144 |
| 11 | + * Dislikes: 97 |
| 12 | + * Total Accepted: 263.6K |
| 13 | + * Total Submissions: 675.2K |
| 14 | + * Testcase Example: '{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}' |
| 15 | + * |
| 16 | + * You are given a perfect binary tree where all leaves are on the same level, |
| 17 | + * and every parent has two children. The binary tree has the following |
| 18 | + * definition: |
| 19 | + * |
| 20 | + * |
| 21 | + * struct Node { |
| 22 | + * int val; |
| 23 | + * Node *left; |
| 24 | + * Node *right; |
| 25 | + * Node *next; |
| 26 | + * } |
| 27 | + * |
| 28 | + * |
| 29 | + * Populate each next pointer to point to its next right node. If there is no |
| 30 | + * next right node, the next pointer should be set to NULL. |
| 31 | + * |
| 32 | + * Initially, all next pointers are set to NULL. |
| 33 | + * |
| 34 | + * |
| 35 | + * |
| 36 | + * Example: |
| 37 | + * |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * Input: |
| 42 | + * {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1} |
| 43 | + * |
| 44 | + * Output: |
| 45 | + * {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":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":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1} |
| 46 | + * |
| 47 | + * Explanation: Given the above perfect binary tree (Figure A), your function |
| 48 | + * should populate each next pointer to point to its next right node, just like |
| 49 | + * in Figure B. |
| 50 | + * |
| 51 | + * |
| 52 | + * |
| 53 | + * |
| 54 | + * Note: |
| 55 | + * |
| 56 | + * |
| 57 | + * You may only use constant extra space. |
| 58 | + * Recursive approach is fine, implicit stack space does not count as extra |
| 59 | + * space for this problem. |
| 60 | + * |
| 61 | + * |
| 62 | + */ |
| 63 | +/* |
| 64 | +// Definition for a Node. |
| 65 | +class Node { |
| 66 | + public int val; |
| 67 | + public Node left; |
| 68 | + public Node right; |
| 69 | + public Node next; |
| 70 | +
|
| 71 | + public Node() {} |
| 72 | +
|
| 73 | + public Node(int _val,Node _left,Node _right,Node _next) { |
| 74 | + val = _val; |
| 75 | + left = _left; |
| 76 | + right = _right; |
| 77 | + next = _next; |
| 78 | + } |
| 79 | +}; |
| 80 | +*/ |
| 81 | +class Solution { |
| 82 | + public Node connect(Node root) { |
| 83 | + if (root == null) return null; |
| 84 | + |
| 85 | + Queue<Node> queue = new LinkedList<>(); |
| 86 | + queue.offer(root); |
| 87 | + |
| 88 | + while (!queue.isEmpty()) { |
| 89 | + int size = queue.size(); |
| 90 | + Node prev = null; |
| 91 | + for (int i = 0; i < size; i++) { |
| 92 | + Node node = queue.poll(); |
| 93 | + if (prev != null) { |
| 94 | + prev.next = node; |
| 95 | + } |
| 96 | + if (node.left != null) { |
| 97 | + queue.offer(node.left); |
| 98 | + } |
| 99 | + if (node.right != null) { |
| 100 | + queue.offer(node.right); |
| 101 | + } |
| 102 | + prev = node; |
| 103 | + } |
| 104 | + prev.next = null; |
| 105 | + } |
| 106 | + |
| 107 | + return root; |
| 108 | + } |
| 109 | +} |
| 110 | + |
0 commit comments