コード例 #1
0
        public TreeNodeWithNext Connect(BinaryTree input)
        {
            TreeNodeWithNext root = input.ConvertToTreeNodeWithNext();

            Connect(root);

            return(root);
        }
コード例 #2
0
ファイル: TestHelper.cs プロジェクト: xisuogu/LeetSharp
 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);
 }
コード例 #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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: TestHelper.cs プロジェクト: xisuogu/LeetSharp
        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(',') + "}");
        }
コード例 #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;
                    }
                }
            }
        }