• Convert Sorted Array to Binary Search Tree


    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

    cpp:

    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            if (nums.empty())
                return nullptr;
            int l = 0;
            int r = nums.size() - 1;
            if (l == r)
                return new TreeNode(nums[l]);
            int m = (r >> 1);
            TreeNode *root = new TreeNode(nums[m]);
    
            helper(l, m - 1, root, nums, 0);
            helper(m + 1, r, root, nums, 1);
            return root;
        }
    
        void helper(int l, int r, TreeNode* root, vector<int>& nums,
                int LeftorRight) {
            if (l > r)
                return;
            if (l == r) {
                if (LeftorRight == 0) {
                    root->left = new TreeNode(nums[l]);
                } else {
                    root->right = new TreeNode(nums[l]);
                }
                return;
            }
            TreeNode *curNode;
            int m = l + ((r - l) >> 1);
            if (LeftorRight == 0) {
                root->left = new TreeNode(nums[m]);
                curNode = root->left;
            } else {
                root->right = new TreeNode(nums[m]);
                curNode = root->right;
            }
            helper(l, m - 1, curNode, nums, 0);
            helper(m + 1, r, curNode, nums, 1);
        }
    };

    java:

    public class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            int len = nums.length;
            if (len == 0)
                return null;
    
            int l = 0;
            int r = len - 1;
            int m = l + ((r - l) >> 1);
    
            if (l == r)
                return new TreeNode(nums[l]);
    
            TreeNode root = new TreeNode(nums[m]);
    
            helper(l, m - 1, root, nums, 0);
            helper(m + 1, r, root, nums, 1);
    
            return root;
    
        }
    
        public void helper(int l, int r, TreeNode root, int[] nums, int LeftorRight) {
            if (l > r)
                return;
            if (l == r) {
                if (LeftorRight == 0)
                    root.left = new TreeNode(nums[l]);
                else
                    root.right = new TreeNode(nums[l]);
                return;
            }
            int m = l + ((r - l) >> 1);
            TreeNode curNode = new TreeNode(nums[m]);
            if (LeftorRight == 0)
                root.left = curNode;
            else
                root.right = curNode;
    
            helper(l, m - 1, curNode, nums, 0);
            helper(m + 1, r, curNode, nums, 1);
    
        }
    }
  • 相关阅读:
    CURL POST提交json类型字符串数据和伪造IP和来源
    windows下nginx的配置
    常用JS兼容问题工具
    无限级分类--Array写法
    JS获取对象指定属性在样式中的信息
    解决IE和Firefox获取来源网址Referer的JS方法
    异步轮询函数
    响应式布局--特殊设备检测
    jQuery Validate校验
    [LeetCode#124]Binary Tree Maximum Path Sum
  • 原文地址:https://www.cnblogs.com/wxquare/p/5211524.html
Copyright © 2020-2023  润新知