108. Convert Sorted Array to Binary Search Tree [Easy]

一、思路和算法

为了构建BST,应该在数组中选取一个数nums[i]用来构建根节点,nums[0, i - 1]用来构建左子树,nums[i + 1, n - 1]用来构建右子树。
为了让二叉树尽量平衡,很自然的想法就是让根节点尽量在数组的中间,让数组左右两边的长度尽量相等。
基于以上的直觉,当数组长度为2k + 1时,根节点选在数组正中间,左右两棵子树都分配k个节点。当数组长度为2k时,根节点选在数组中间靠左的位置,左子树分配k - 1个节点,右子树分配k个节点。

二、证明

很显然,上面的算法构建的是二叉查找树,对于任意一个节点,左子树的值都小于根节点的值,右子树的值都大于根节点的值。
现在我们利用数学归纳法证明上面给出的算法构建的是平衡树:

当数组长度<= 2时,很容易验证构造的是平衡树。
假设数组长度< k时构造的是平衡树,那么可以证明数组长度= k时构造的也是平衡树。
记左子树为l,右子树为r;左子树的左右子树分别为lllr;右子树的左右子树分别为 rlrr。树tree的节点个数我们用|tree|来表示,高度用h(tree)来表示。
1)|l| < k|r| < k,根据假设可知l和r均为平衡树。
2)若k为奇数,那么|l| == |r|,构造算法也相同,所以l和r是两棵结构完全相同的树,所以当前构造的二叉树为平衡树。
3)若k为偶数,那么|l| + 1 = |r|。记|l| = x,那么|r| = x + 1
x为奇数时,有|ll| = |lr| = |rl| = y, |rr| = y + 1。所以0 <= h(rr) - h(rl),又由于r为平衡树,所以h(rr) - h(rl) <= 1
h(l) = h(ll) + 1 = h(rl) + 1,h(r) = h(rr) + 1,所以有0 <= h(r) - h(l) <= 1,所以当前构造的是平衡树。
x为偶数时,通过类似的分析,同样能推出当前构造的是平衡树。

三、完整代码

class Solution {
    private int[] nums;

    public TreeNode sortedArrayToBST(int[] nums) {
        this.nums = nums;
        return sArray2BST(0, nums.length - 1);
    }

    private TreeNode sArray2BST(int left, int right) {
        if (left > right) return null;
        if (left == right) return new TreeNode(nums[left]);

        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = sArray2BST(left, mid - 1);
        root.right = sArray2BST(mid + 1, right);
        return root;
    }
}
【Solution】 To convert a binary search tree into a sorted circular doubly linked list, we can use the following steps: 1. Inorder traversal of the binary search tree to get the elements in sorted order. 2. Create a doubly linked list and add the elements from the inorder traversal to it. 3. Make the list circular by connecting the head and tail nodes. 4. Return the head node of the circular doubly linked list. Here's the Python code for the solution: ``` class Node: def __init__(self, val): self.val = val self.prev = None self.next = None def tree_to_doubly_list(root): if not root: return None stack = [] cur = root head = None prev = None while cur or stack: while cur: stack.append(cur) cur = cur.left cur = stack.pop() if not head: head = cur if prev: prev.right = cur cur.left = prev prev = cur cur = cur.right head.left = prev prev.right = head return head ``` To verify the accuracy of the code, we can use the following test cases: ``` # Test case 1 # Input: [4,2,5,1,3] # Output: # Binary search tree: # 4 # / \ # 2 5 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5 # Doubly linked list in reverse order: 5 <-> 4 <-> 3 <-> 2 <-> 1 root = Node(4) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) # Test case 2 # Input: [2,1,3] # Output: # Binary search tree: # 2 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 # Doubly linked list in reverse order: 3 <-> 2 <-> 1 root = Node(2) root.left = Node(1) root.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) ``` The output of the test cases should match the expected output as commented in the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值