Ejemplo n.º 1
0
        public static RNode ConnectToRightLevelOrder(RNode root)
        {
            if (root == null) return null;
            var ret = root;
            var q = new Queue<RNode>();
            q.Enqueue(root);
            q.Enqueue(null);
            while (q.Count > 1)
            {
                root = q.Dequeue();
                if (root == null) { q.Enqueue(null); continue;}
                root.NextRight = q.Peek();
                if (root.Left != null) { q.Enqueue(root.Left); }
                if (root.Right != null) { q.Enqueue(root.Right); }
            }

            return ret;
        }
Ejemplo n.º 2
0
 public void Connect_nodes_in_level_Binary_Tree()
 {
     var root = new RNode(1);
     root.Left = new RNode(2);
     root.Right = new RNode(3);
     root.Left.Left = new RNode(4);
     root.Left.Right = new RNode(5);
     root.Right.Left = new RNode(6);
     root.Right.Right = new RNode(7);
     var res = BinaryTree.ConnectToRightLevelOrder(root);
     Assert.IsNull(res.NextRight);
     Assert.IsNull(res.Right.NextRight);
     Assert.AreEqual(res.Left.NextRight.Key, 3);
     Assert.AreEqual(res.Left.Left.NextRight.Key, 5);
     Assert.AreEqual(res.Left.Right.NextRight.Key, 6);
     Assert.AreEqual(res.Right.Left.NextRight.Key, 7);
 }