Beispiel #1
0
 public TreeLinkNode GetNext(TreeLinkNode pNode)
 {
     // write code here
     if (pNode == null)
     {
         return(null);
     }
     else if (pNode.right != null) // 有右子节点, 找最左节点。
     {
         pNode = pNode.right;
         while (pNode.left != null)
         {
             pNode = pNode.left;
         }
         return(pNode);
     }
     else
     {
         if (pNode.next != null && pNode.next.left == pNode)
         {
             return(pNode.next);                                              // 为左子节点,找父节点。
         }
         else if (pNode.next != null && pNode.next.right == pNode)
         {
             while (pNode.next != null && pNode.next.left != pNode)
             {
                 pNode = pNode.next;
             }
             return(pNode.next);
         }//为右节点,向上遍历找到左子节点为该父节点的节点
         return(pNode.next);
     }
 }
Beispiel #2
0
        public TreeLinkNode GetNext(TreeLinkNode pNode)
        {
            // write code here
            if (pNode == null)
            {
                return(null);
            }
            if (pNode.right != null)      //如果有右子树,则找右子树的最左节点
            {
                pNode = pNode.right;
                while (pNode.left != null)
                {
                    pNode = pNode.left;
                }
                return(pNode);
            }

            while (pNode.next != null)   //没右子树,则找第一个当前节点是父节点左孩子的节点的父节点
            {
                if (pNode.next.left == pNode)
                {
                    return(pNode.next);
                }
                pNode = pNode.next;
            }
            return(null);   //退到了根节点仍没找到,则返回null
        }
Beispiel #3
0
        public static TreeLinkNode GenerateLinkTree(int?[] nums)
        {
            if (nums == null || nums.Length == 0)
            {
                return(null);
            }

            var i     = 0;
            var first = new TreeLinkNode(nums[i++].Value);
            var queue = new Queue <TreeLinkNode>();

            queue.Enqueue(first);

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                if (i < nums.Length && nums[i].HasValue)
                {
                    var node = new TreeLinkNode(nums[i].Value);
                    current.left = node;
                    queue.Enqueue(node);
                }
                if (i + 1 < nums.Length && nums[i + 1].HasValue)
                {
                    var node = new TreeLinkNode(nums[i + 1].Value);
                    current.right = node;
                    queue.Enqueue(node);
                }
                i += 2;
            }

            return(first);
        }
            TreeLinkNode Insert(int value)
            {
                TreeLinkNode nextTry = null;

                if (value > val)
                {
                    if (right == null)
                    {
                        right      = new TreeLinkNode(value);
                        right.next = this;
                        return(right);
                    }
                    else
                    {
                        nextTry = right;
                    }
                }

                if (value < val)
                {
                    if (left == null)
                    {
                        left      = new TreeLinkNode(value);
                        left.next = this;
                        return(left);
                    }
                    else
                    {
                        nextTry = left;
                    }
                }

                return(nextTry.Insert(value));
            }
Beispiel #5
0
        //分析二叉树的下一个节点,一共有以下情况:
        //1.二叉树为空,则返回空;
        //2.节点右孩子存在,则设置一个指针从该节点的右孩子出发,一直沿着指向左子结点的指针找到的叶子节点即为下一个节点;
        //3.节点不是根节点。如果该节点是其父节点的左孩子,则返回父节点;否则继续向上遍历其父节点的父节点,重复之前的判断,返回结果。

        /// <summary>
        /// 057 二叉树的下一个结点
        /// </summary>
        /// <param name="pNode"></param>
        /// <returns></returns>
        public TreeLinkNode GetNext(TreeLinkNode pNode)
        {
            // 1.
            if (pNode == null)
            {
                return(null);
            }

            // 2.
            if (pNode.right != null)
            {
                pNode = pNode.right;
                while (pNode.left != null)
                {
                    pNode = pNode.left;
                }
                return(pNode);
            }

            // 3.
            while (pNode.next != null)
            {
                TreeLinkNode pRoot = pNode.next;
                if (pRoot.left == pNode)
                {
                    return(pRoot);
                }
                pNode = pNode.next;
            }
            return(null);
        }
        public void TestSample2()
        {
            var root = TreeLinkNode.ConstructFromArray(new int?[] { 1, 2, 3, 4, 5, 6, 7 });

            new Solution().connect(root);
            Assert.AreEqual(6, root.left.right.next.val);
        }
        public TreeLinkNode Connect(TreeLinkNode root)
        {
            TreeLinkNode levelStart = root;

            while (levelStart != null)
            {
                TreeLinkNode cur = levelStart;
                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;
                }
                levelStart = levelStart.left;
            }

            return(root);
        }
    // 递归法
    // 时间复杂度O(n),空间复杂度O(n)
    public void Connect(TreeLinkNode root)
    {
        if (root == null)
        {
            return;
        }
        Queue <TreeLinkNode> queue = new Queue <TreeLinkNode>();

        queue.Enqueue(root);
        while (queue.Count > 0)
        {
            // 这一层是叶子节点
            if (queue.Peek().left == null)
            {
                break;
            }
            // 每层开始初始化前一个兄弟节点为空
            TreeLinkNode pre  = null;
            int          size = queue.Count;
            for (int i = 0; i < size; i++)
            {
                TreeLinkNode cur = queue.Dequeue();
                // 前一个兄弟节点的右节点next指针指向当前节点的左节点
                if (pre != null)
                {
                    pre.right.next = cur.left;
                }
                // 左节点的next指针指向右节点
                cur.left.next = cur.right;
                pre           = cur;
                queue.Enqueue(cur.left);
                queue.Enqueue(cur.right);
            }
        }
    }
Beispiel #9
0
        public void ConnectII2(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }

            Queue <TreeLinkNode> queue = new Queue <TreeLinkNode>();

            queue.Enqueue(root);

            while (queue.Count() > 0)
            {
                int size = queue.Count();

                TreeLinkNode next = null;

                for (int indx = 0; indx < size; indx++)
                {
                    TreeLinkNode currNode = queue.Dequeue();

                    currNode.next = next;
                    next          = currNode;

                    if (currNode.right != null)
                    {
                        queue.Enqueue(currNode.right);
                    }
                    if (currNode.left != null)
                    {
                        queue.Enqueue(currNode.left);
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Level traversal
        /// </summary>
        /// <param name="root"></param>
        public void ConnectTwo(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }

            while (root != null)
            {
                var temp = root;
                if (root.left == null)
                {
                    break;
                }

                while (root != null)
                {
                    root.left.next = root.right;
                    if (root.right != null && root.next != null)
                    {
                        root.right.next = root.next.left;
                    }

                    root = root.next;
                }

                root = temp.left;
            }
        }
Beispiel #11
0
    public void Connect(TreeLinkNode root)
    {
        var currLevel = new List <TreeLinkNode>();
        var nextLevel = new List <TreeLinkNode>();

        if (root != null)
        {
            currLevel.Add(root);
        }

        while (currLevel.Count > 0)
        {
            for (int i = 0; i < currLevel.Count - 1; i++)
            {
                var cur = currLevel[i];
                cur.next = currLevel[i + 1];
                if (cur.left != null)
                {
                    nextLevel.Add(cur.left);
                }
                if (cur.right != null)
                {
                    nextLevel.Add(cur.right);
                }
            }

            currLevel = nextLevel;
            nextLevel = new List <TreeLinkNode>();
        }
    }
    public void Connect(TreeLinkNode root)
    {
        if (root == null)
        {
            return;
        }
        Queue <TreeLinkNode> curr = new Queue <TreeLinkNode>();
        Queue <TreeLinkNode> next = new Queue <TreeLinkNode>();

        curr.Enqueue(root);
        while (curr.Count != 0)
        {
            TreeLinkNode temp = curr.Dequeue();
            if (temp.left != null)
            {
                next.Enqueue(temp.left);
            }
            if (temp.right != null)
            {
                next.Enqueue(temp.right);
            }
            if (curr.size() != 0)
            {
                temp.next = curr.Peek();
            }
            else
            {
                foreach (TreeLinkNode node in next)
                {
                    curr.Enqueue(node);
                }
                next.Clear();
            }
        }
    }
Beispiel #13
0
        // Medium 116 https://leetcode.com/problems/populating-next-right-pointers-in-each-node
        public void Connect(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }

            TreeLinkNode lvlStart = root;
            TreeLinkNode curNode  = null;

            while (lvlStart.left != null)
            {
                curNode = lvlStart;

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

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

                lvlStart = lvlStart.left;
            }
        }
Beispiel #14
0
        static void Test(TreeLinkNode root)
        {
            var solution = new Solution();

            solution.Connect(root);
            TreeLinkNode.ShowTree(root);
        }
    // 时间复杂度O(n),空间复杂度O(1)
    public void Connect(TreeLinkNode root)
    {
        if (root == null)
        {
            return;
        }

        // 当前层的节点
        TreeLinkNode cur = root;
        // 下一层的第一个节点
        TreeLinkNode nextLevel = cur.left;

        // 当下一层的节点不为空
        while (nextLevel != null)
        {
            // 当前左节点的next指针指向右节点
            cur.left.next = cur.right;
            // 如果当前节点还不是这一层的最后一个节点,
            // 则当前节点右节点的next指针指向这一层下一个节点的左节点
            if (cur.next != null)
            {
                cur.right.next = cur.next.left;
                cur            = cur.next;
            }
            else
            {
                // 如果到了这一层的最后一个节点,把cur指向下一层的第一个节点
                cur = nextLevel;
                // 更新下一层的第一个节点
                nextLevel = cur.left;
            }
        }
    }
            public TreeLinkNode GetNext(TreeLinkNode pNode)
            {
                if (pNode == null)
                {
                    throw new System.ArgumentNullException(nameof(pNode));
                }

                TreeLinkNode result = null;
                var          root   = GetRoot(pNode);

                GetNext(root, (node) => {
                    if (node.val > pNode.val)
                    {
                        if (result == null)
                        {
                            result = node;
                        }
                        if (result != null && result.val > node.val)
                        {
                            result = node;
                        }
                    }
                });

                return(result);
            }
Beispiel #17
0
 static void Main(string[] args)
 {
     //Test(TreeLinkNode.GetTree());
     Console.WriteLine();
     Test(TreeLinkNode.GetTree2());
     Console.ReadKey(true);
 }
Beispiel #18
0
        // Medium 117 https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii
        // Un balenced

        public void ConnectII(TreeLinkNode root)
        {
            while (root != null)
            {
                TreeLinkNode tempChild    = new TreeLinkNode(0);
                TreeLinkNode currentChild = tempChild;

                while (root != null)
                {
                    if (root.left != null)
                    {
                        currentChild.next = root.left;
                        currentChild      = currentChild.next;
                    }

                    if (root.right != null)
                    {
                        currentChild.next = root.right;
                        currentChild      = currentChild.next;
                    }

                    root = root.next;
                }

                root = tempChild.next;
            }
        }
Beispiel #19
0
    // Discuss solution
    public void connect_(TreeLinkNode root)
    {
        if (root == null)
        {
            return;
        }
        TreeLinkNode curP          = root;
        TreeLinkNode nextDummyHead = new TreeLinkNode(0);
        TreeLinkNode p             = nextDummyHead;

        while (curP != null)
        {
            if (curP.left != null)
            {
                p.next = curP.left;
                p      = p.next;
            }
            if (curP.right != null)
            {
                p.next = curP.right;
                p      = p.next;
            }
            if (curP.next != null)
            {
                curP = curP.next;
            }
            else
            {
                curP = nextDummyHead.next;
                nextDummyHead.next = null;
                p = nextDummyHead;
            }
        }
    }
            public static void Test()
            {
                TestCase(TreeLinkNode.Create(new int[] { 8, 6, 10, 5, 7, 9, 11 }, 8));
                //TestCase(TreeLinkNode.Create(new int[] { 8, 6, 10, 5, 7, 9, 11 }, 7));

                System.Console.ReadKey();
            }
 public void Connect(TreeLinkNode root) {
     if(root==null) return; //edge case: input root is null
     if(root.left==null) return; //Base case: current root has no children
     root.left.next=root.right;
     if(root.next!=null)
         root.right.next = root.next.left;
     Connect(root.left);
     Connect(root.right);
 }
Beispiel #22
0
        public void connect(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }

            helper(root.left, root.right);
        }
            TreeLinkNode GetRoot(TreeLinkNode node)
            {
                if (node.next == null)
                {
                    return(node);
                }

                return(GetRoot(node.next));
            }
            static void TestCase(TreeLinkNode input)
            {
                var start  = System.DateTime.Now;
                var obj    = new Solution();
                var result = obj.GetNext(input);
                var elapse = (System.DateTime.Now - start).TotalMilliseconds;

                System.Console.WriteLine($"{result} -- with {elapse}ms");
            }
Beispiel #25
0
        public void Connect(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }

            TreeLinkNode lastHead    = root; //prevous level's head
            TreeLinkNode lastCurrent = null; //previous level's pointer
            TreeLinkNode currentHead = null; //currnet level's head
            TreeLinkNode current     = null; //current level's pointer

            while (lastHead != null)
            {
                lastCurrent = lastHead;

                while (lastCurrent != null)
                {
                    //left child is not null
                    if (lastCurrent.left != null)
                    {
                        if (currentHead == null)
                        {
                            currentHead = lastCurrent.left;
                            current     = lastCurrent.left;
                        }
                        else
                        {
                            current.next = lastCurrent.left;
                            current      = current.next;
                        }
                    }

                    //right child is not null
                    if (lastCurrent.right != null)
                    {
                        if (currentHead == null)
                        {
                            currentHead = lastCurrent.right;
                            current     = lastCurrent.right;
                        }
                        else
                        {
                            current.next = lastCurrent.right;
                            current      = current.next;
                        }
                    }

                    lastCurrent = lastCurrent.next;
                }

                //update last head
                lastHead    = currentHead;
                currentHead = null;
            }
        }
 void GetNext(TreeLinkNode node, System.Action <TreeLinkNode> func)
 {
     if (node == null)
     {
         return;
     }
     GetNext(node.left, func);
     func(node);
     GetNext(node.right, func);
 }
Beispiel #27
0
        private void helper(TreeLinkNode node1, TreeLinkNode node2)
        {
            if (node1 == null)
            {
                return;
            }

            node1.next = node2;
            helper(node1.left, node1.right);
            helper(node2.left, node2.right);
            helper(node1.right, node2.left);
        }
Beispiel #28
0
 private void CheckChild(TreeLinkNode node)
 {
     if (node != null)
     {
         if (pre != null)
         {
             pre.next = node;
         }
         pre = node;
         queue.Enqueue(node);
     }
 }
Beispiel #29
0
        private TreeLinkNode BuildNotPerfacet()
        {
            var root = new TreeLinkNode(1);

            root.Left  = new TreeLinkNode(2);
            root.Right = new TreeLinkNode(3);

            root.Left.Left  = new TreeLinkNode(4);
            root.Left.Right = new TreeLinkNode(5);

            root.Right.Right = new TreeLinkNode(7);
            return(root);
        }
Beispiel #30
0
        public static void Connect_perfact_loop(TreeLinkNode root)
        {
            if (root == null)
            {
                return;
            }
            var queue = new Queue <TreeLinkNode>();

            queue.Enqueue(root);
            root.Next = null;
            while (queue.Count > 0)
            {
                var size = queue.Count;
                // var list= new List<TreeLinkNode>();

                for (var i = 1; i <= size; i++)
                {
                    var node = queue.Peek();
                    queue.Dequeue();
                    if (i <= size - 1)
                    {
                        node.Next = queue.Peek();
                    }

                    if (node.Left != null)
                    {
                        queue.Enqueue(node.Left);
                    }
                    if (node.Right != null)
                    {
                        queue.Enqueue(node.Right);
                    }
                }

                //for (var i = 1; i <= size; i++)
                //{
                //    var node = queue.Dequeue();
                //    list.Add(node);
                //    if(node.Left != null)  queue.Enqueue(node.Left);
                //    if (node.Right != null) queue.Enqueue(node.Right);
                //}
                //for (var i = 0; i < size; i++)
                //{
                //    if (i < size - 1)
                //        list[i].Next = list[i + 1];
                //    else
                //        list[i].Next =  null;
                //}
            }
        }
Beispiel #31
0
        public void TestMethod1()
        {
            // Arrange
            PopulatingNextRightPointersinEachNodeII question = new PopulatingNextRightPointersinEachNodeII();
            TreeLinkNode root = new TreeLinkNode(1);

            root.left           = new TreeLinkNode(2);
            root.left.left      = new TreeLinkNode(3);
            root.left.left.left = new TreeLinkNode(4);
            root.right          = new TreeLinkNode(5);

            // Act
            question.Connect(root);
        }
    void connect(TreeLinkNode root)
    {
        if(root == null || root.left == null) {
            return;
        }
        root.left.next = root.right;
        if(root.next != null) {
            root.right.next = root.next.left;
        }

        connect(root.left);
        connect(root.right);

        return;
    }
 public void Connect(TreeLinkNode root) {
     if(root==null) return;
     Queue<TreeLinkNode> curr = new Queue<TreeLinkNode>();
     Queue<TreeLinkNode> next = new Queue<TreeLinkNode>();
     curr.Enqueue(root);
     while(curr.Count!=0){
         TreeLinkNode temp = curr.Dequeue();
         if(temp.left!=null)
             next.Enqueue(temp.left);
         if(temp.right!=null)
             next.Enqueue(temp.right);
         if(curr.size()!=0)
             temp.next = curr.Peek();
         else{
             foreach(TreeLinkNode node in next)
                 curr.Enqueue(node);
             next.Clear();
         }
     }
 }