package com.app.main.LeetCode.tree;
import com.app.main.LeetCode.base.Node;
/**
*
* 117
*
* medium
*
* https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
*
* Given a binary tree
*
* struct Node {
* int val;
* Node *left;
* Node *right;
* Node *next;
* }
* Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
*
* Initially, all next pointers are set to NULL.
*
*
*
* Example:
*
*
*
* Input: {"$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":null,"next":null,"right":{"$id":"6","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}
*
* Output: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":null,"right":null,"val":7},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"6","left":null,"next":null,"right":{"$ref":"5"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"6"},"val":1}
*
* Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.
*
*
* Note:
*
* You may only use constant extra space.
* Recursive approach is fine, implicit stack space does not count as extra space for this problem.
*
*
* Created with IDEA
* author:Dingsheng Huang
* Date:2019/11/19
* Time:下午7:28
*/
public class PopulatingNextRightPointersInEachNodeII {
public Node connect(Node root) {
Node base = new Node();
Node pre = base;
Node curr = root;
while (curr != null) {
if (curr.left != null) {
pre.next = curr.left;
pre = pre.next;
}
if (curr.right != null) {
pre.next = curr.right;
pre = pre.next;
}
curr = curr.next;
if (curr == null) {
curr = base.next;
pre = base;
base.next = null;
}
}
return root;
}
}
/**
* {"$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":null,"next":null,"right":null,"val":3},"val":1}
*/