public RNode Connect(RNode root) //perfect binary tree
        {
            #region O(1) space

            if (root == null || root.left == null)
            {
                return(root);
            }

            RNode parent = root;

            while (parent.left != null)
            {
                RNode current = parent;

                while (current != null)
                {
                    current.left.next = current.right;

                    if (current.next != null)
                    {
                        current.right.next = current.next.left;
                    }

                    current = current.next;
                }


                parent = parent.left;
            }

            return(root);

            #endregion
        }
 public RNode(int _val, RNode _left, RNode _right, RNode _next)
 {
     val   = _val;
     left  = _left;
     right = _right;
     next  = _next;
 }
        public RNode Connect2(RNode root) // if it is not perfect binary tree -> solve again
        {
            #region O(N) space
            //if (root == null)
            //    return root;

            //Queue<RNode> queue = new Queue<RNode>();
            //queue.Enqueue(root);


            //while (queue.Count > 0)
            //{
            //    int count = queue.Count;
            //    RNode prev = null;

            //    while (count > 0)
            //    {
            //        RNode node = queue.Dequeue();

            //        if (prev != null)
            //            prev.next = node;

            //        prev = node;

            //        if (node.left != null)
            //            queue.Enqueue(node.left);

            //        if (node.right != null)
            //            queue.Enqueue(node.right);


            //        count--;
            //    }


            //}

            //return root;

            #endregion

            if (root == null)
            {
                return(root);
            }

            RNode head = root;

            while (head != null)
            {
                RNode curr          = null;
                RNode nextLevelHead = null;

                while (head != null)
                {
                    if (head.left != null)
                    {
                        if (curr == null)
                        {
                            curr          = head.left;
                            nextLevelHead = head.left;
                        }
                        else
                        {
                            curr.next = head.left;
                            curr      = curr.next;
                        }
                    }

                    if (head.right != null)
                    {
                        if (curr == null)
                        {
                            curr          = head.right;
                            nextLevelHead = head.right;
                        }
                        else
                        {
                            curr.next = head.right;
                            curr      = curr.next;
                        }
                    }

                    head = head.next;
                }

                head = nextLevelHead;
            }

            return(root);
        }