Esempio n. 1
0
        public static MyTreeNode <int> S4_2_MinimalTree(int[] arr, int start, int end)
        {
            if (start > end)
            {
                return(null);
            }
            if (start == end)
            {
                return(new MyTreeNode <int>(arr[start]));
            }

            var midPoint = (start + end) / 2;
            var root     = new MyTreeNode <int>(midPoint);

            root.LeftChild  = S4_2_MinimalTree(arr, start, midPoint - 1);
            root.RightChild = S4_2_MinimalTree(arr, midPoint + 1, end);
            return(root);
        }
Esempio n. 2
0
        public static MyTreeNode <int> S4_2_MinimalTree2(int[] arr)
        {
            if (arr.Length == 0)
            {
                return(null);
            }
            if (arr.Length == 1)
            {
                return(new MyTreeNode <int>(arr[0]));
            }

            var mid  = arr.Length / 2;
            var root = new MyTreeNode <int>(arr[mid]);

            root.LeftChild  = S4_2_MinimalTree2(arr.Take(mid).ToArray());
            root.RightChild = S4_2_MinimalTree2(arr.Skip(mid).ToArray());

            return(root);
        }