Beispiel #1
0
        private static TreeNode Create(int[] sortedArray, int left, int right)
        {
            if (left > right)
            {
                return(null);
            }
            var mid        = left + (right - left) / 2;
            var parentNode = new TreeNode(sortedArray[mid]);

            parentNode.SetLeftChild(Create(sortedArray, left, mid - 1));
            parentNode.SetRightChild(Create(sortedArray, mid + 1, right));
            return(parentNode);
        }
Beispiel #2
0
        private static TreeNode CreateMinimalBST(int[] array, int start, int end)
        {
            if (end < start)
            {
                return(null);
            }

            var mid    = start + (end - start) / 2;
            var parent = new TreeNode(array[mid]);

            parent.SetLeftChild(CreateMinimalBST(array, start, mid - 1));
            parent.SetRightChild(CreateMinimalBST(array, mid, end));

            return(parent);
        }