public TreeNodeWithNext Connect(BinaryTree input)
        {
            TreeNodeWithNext root = input.ConvertToTreeNodeWithNext();

            Connect(root);

            return(root);
        }
Esempio n. 2
0
 public static TreeNodeWithNext ConvertToTreeNodeWithNext(this BinaryTree root)
 {
     if (root != null)
     {
         TreeNodeWithNext newRoot = new TreeNodeWithNext(root.Value);
         newRoot.Left  = ConvertToTreeNodeWithNext(root.Left);
         newRoot.Right = ConvertToTreeNodeWithNext(root.Right);
         return(newRoot);
     }
     return(null);
 }
Esempio n. 3
0
        private void Connect(TreeNodeWithNext root)
        {
            if (root == null)
            {
                return;
            }

            Connect(root.Left, root);
            Connect(root.Right, root);

            Connect(root.Right);
            Connect(root.Left);
        }
        private void Connect(TreeNodeWithNext root)
        {
            if (root == null)
            {
                return;
            }

            if (root.Left == null && root.Right == null)
            {
                return;
            }

            root.Left.Next = root.Right;
            if (root.Next != null)
            {
                root.Right.Next = root.Next.Left;
            }

            Connect(root.Left);
            Connect(root.Right);
        }
Esempio n. 5
0
        public static string SerializeTreeNodeWithNext(this TreeNodeWithNext root)
        {
            if (root == null)
            {
                return("{}");
            }
            StringBuilder sb = new StringBuilder();

            sb.Append("{");
            var currentLevel           = root;
            TreeNodeWithNext nextLevel = null;

            while (currentLevel != null)
            {
                while (currentLevel != null)
                {
                    if (nextLevel == null)
                    {
                        if (currentLevel.Left != null)
                        {
                            nextLevel = currentLevel.Left;
                        }
                        else if (currentLevel.Right != null)
                        {
                            nextLevel = currentLevel.Right;
                        }
                    }
                    sb.Append(currentLevel.Value + ",");
                    currentLevel = currentLevel.Next;
                }
                sb.Append("#,");
                currentLevel = nextLevel;
                nextLevel    = null;
            }
            string result = sb.ToString();

            return(result.TrimEnd(',') + "}");
        }
Esempio n. 6
0
        private void Connect(TreeNodeWithNext current, TreeNodeWithNext parent)
        {
            if (current == null)
            {
                return;
            }

            if (current != parent.Right && parent.Right != null)
            {
                current.Next = parent.Right;
            }
            else
            {
                while (parent.Next != null)
                {
                    parent = parent.Next;
                    if (parent.Left != null || parent.Right != null)
                    {
                        current.Next = parent.Left == null ? parent.Right : parent.Left;
                        break;
                    }
                }
            }
        }