Пример #1
0
 public Node1(int _val, Node1 _left, Node1 _right, Node1 _next)
 {
     val   = _val;
     left  = _left;
     right = _right;
     next  = _next;
 }
Пример #2
0
        //TC - O(N)
        //SC - O(1), no space
        public Node1 Connect2(Node1 root)
        {
            if (root == null)
            {
                return(root);
            }

            Node1 leftMost = root;

            while (leftMost.left != null)
            {
                Node1 head = leftMost;
                while (head != null)
                {
                    //Condition 1: for two childs of same parent Node1
                    head.left.next = head.right;

                    //condition 2: right child of Node11 and left child of Node11's sibling(Node12)
                    if (head.next != null)
                    {
                        head.right.next = head.next.left;
                    }

                    head = head.next;
                }
                leftMost = leftMost.left;
            }

            return(root);
        }
Пример #3
0
        /**************************************II- Binary Tree with missing Nodes********************************************************/
        //TC - O(N)
        //SC - O(N)
        //USING BFS Level ORDER
        //SAME AS Connect above, useing q and adding connection of the Dequeue nodes


        //TC - O(N)
        //SC - O(1)
        public Node1 ConnectII_I(Node1 root)
        {
            if (root == null)
            {
                return(root);
            }
            //head node for return purpose only
            Node1 head = root;

            //dummy will maintain the child level order connections
            Node1 dummy = new Node1(0);
            Node1 pre   = dummy;

            //processing on root
            while (root != null)
            {
                //if left exists then add it to pre
                if (root.left != null)
                {
                    pre.next = root.left;
                    pre      = pre.next;
                }
                //if right exists add it to the pre whcih alreay have left init
                if (root.right != null)
                {
                    pre.next = root.right;
                    pre      = pre.next;
                }
                root = root.next;
                //if root is null which means the that level is end, so move root to next node of dummy(make null)
                if (root == null)
                {
                    //reset all
                    pre        = dummy;
                    root       = dummy.next;
                    dummy.next = null;
                }
            }
            return(head);
        }
Пример #4
0
        //TC - O(N)
        //SC - O(N)
        //USING BFS Level ORDER
        public Node1 Connect(Node1 root)
        {
            if (root == null)
            {
                return(root);
            }

            Queue <Node1> q = new Queue <Node1>();

            q.Enqueue(root);

            while (q.Count > 0)
            {
                int size = q.Count;
                for (int i = 0; i < size; i++)
                {
                    Node1 Node1 = q.Dequeue();

                    if (i < size - 1)//i should be less than size to access the same level Node1s in the q
                    {
                        Node1.next = q.Peek();
                    }

                    if (Node1.left != null)
                    {
                        q.Enqueue(Node1.left);
                    }
                    if (Node1.right != null)
                    {
                        q.Enqueue(Node1.right);
                    }
                }
            }

            return(root);
        }