【Lintcode】177. Convert Sorted Array to Binary Search Tree With Minimal Height

Title address:

https://www.lintcode.com/problem/convert-sorted-array-to-binary-search-tree-with-minimal-height/description

Given an ascending array A A , to convert it into a BST, requires the height to reach the minimum.

Can be solved with recursion. take A [ m i d ] A[mid] as the root of the tree, A [ m i d ] A[mid] A A middle number, then the left subtree and recursively established right subtree, can be connected to the root. code show as below:

public class Solution {
    /*
     * @param A: an integer array
     * @return: A tree node
     */
    public TreeNode sortedArrayToBST(int[] A) {
        // write your code here
        return buildTree(A, 0, A.length - 1);
    }
    
    private TreeNode buildTree(int[] A, int l, int r) {
        if (l > r) {
            return null;
        }
        
        int m = l + (r - l >> 1);
        TreeNode root = new TreeNode(A[m]);
        root.left = buildTree(A, l, m - 1);
        root.right = buildTree(A, m + 1, r);
        return root;
    }
}

class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int x) {
        val = x;
    }
}

time complexity O ( n ) O (n) , space O ( log n ) O(\log n)

First of all, we can use mathematical induction to prove that the established tree is indeed BST. Then we first prove that the tree is balanced (meaning that for each of its subtrees, the absolute value of the difference between the number of left and right subtree nodes does not exceed 1 1 ). Mathematical induction: when [ l , r ] = 0 |[l,r]|=0 or [ l , r ] = 1 |[l,r]|=1 conclusion is established. Assuming conclusions for [ l , r ] = k |[l,r]|=k established at , when [ l , r ] = k + 1 |[l,r]|=k+1 if k + 1 k+1 is odd, then m m is just the index of the number in the center of the array. According to mathematical induction, the left and right subtrees are balanced, and the entire tree is also balanced, so the conclusion is true; k + 1 k+1 is even, then m m takes the index of the left-most number in the center of the array, and the number of nodes in the left subtree is smaller than that in the right subtree 1 1 , still right. So the conclusion is to any n n is right.

Then we prove that even for a general tree, when the tree is balanced, its height is the smallest. It can also be proved by mathematical induction. When the number of nodes in the tree is 0 , 1 , 2 0,1,2 conclusion is true at o'clock; k k The tree conclusion of nodes is also established, when the tree has k + 1 k+1 node, suppose its left and right subtrees have u in sum v v nodes, then obviously by mathematical induction, when the left and right subtrees are balanced, the height of the entire tree is the smallest. Let the root of the tree be r o o t root , the left and right subtrees are l l and r r , set d ( r o o t ) d(root) means r o o t root is the height of the rooted tree. in case u v > 1 u-v>1 , then there is u > u 1 v + 1 u>u-1\ge v+1 , so d ( r ) = 1 + max ( d ( l ) , d ( r ) ) d(r)=1+\max(d(l),d(r)) Assuming that the left subtree is moved one node to the right subtree, the left and right subtrees are respectively l l' And r r' , The number of nodes in the left subtree is still more than that in the right subtree by at least 1 1 , so d ( r ) d ( r ) d ( l ) d ( l ) d (r) \ le d (r ') \ le d (l') \ le d (l) , that is, the height of the tree will become smaller. So when the tree is balanced, its height is the smallest.

Published 387 original articles · liked 0 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/105465819