首页 > 其他分享 >108. 将有序数组转换为二叉搜索树(leetcode)

108. 将有序数组转换为二叉搜索树(leetcode)

时间:2024-05-15 16:52:19浏览次数:24  
标签:node TreeNode nums int dfs 二叉 108 root leetcode

https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/

要点是分割左右区间,并且分割时需要注意left和right相加可能会超过int,但是本题不需要

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        // 有返回值写法
        TreeNode root = dfs(nums,0,nums.length);
        return root;
    }

    // 构建平衡二叉树节点,若nums仍然有余数,则继续遍历构建节点,区间左闭右开
    TreeNode dfs(int[] nums,int start,int end)
    {
        if(start>=end)return null;
        // 单层逻辑,每次取出区间中间值进行bulid,
        int mid=start+end >> 1;
        TreeNode node = new TreeNode(nums[mid]);
        // 建立连接,并构建左右子树
        node.left = dfs(nums,start,mid);
        node.right = dfs(nums,mid+1,end);
        return node;
    }
}



class Solution {
    TreeNode root;
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length>0)root = new TreeNode(nums[nums.length/2]);
        // 无返回值写法
        dfs(root,nums,0,nums.length/2);
        dfs(root,nums,nums.length/2+1,nums.length);
        return root;
    }

    // 构建平衡二叉树节点,若nums仍然有余数,则继续遍历构建节点,区间左闭右开
    void dfs(TreeNode root,int[] nums,int start,int end)
    {
        if(start>=end)return;
        // 单层逻辑,每次取出区间中间值进行bulid,
        int mid=start+end >> 1;
        TreeNode node = new TreeNode(nums[mid]);
        // 建立连接
        if(node.val > root.val)root.right = node;
        else root.left = node;
        // 构建左右子树
        dfs(node,nums,start,mid);
        dfs(node,nums,mid+1,end);
    }
}

 

标签:node,TreeNode,nums,int,dfs,二叉,108,root,leetcode
From: https://www.cnblogs.com/lxl-233/p/18194245

相关文章

  • LeetCode 1992. Find All Groups of Farmland
    原题链接在这里:https://leetcode.com/problems/find-all-groups-of-farmland/description/题目:Youaregivena 0-indexed mxn binarymatrix land wherea 0 representsahectareofforestedlandanda 1 representsahectareoffarmland.Tokeepthelandor......
  • [LeetCode] Find the Minimum Cost Array Permutation
    Youaregivenanarray nums whichisa permutation of [0,1,2,...,n-1].The score ofanypermutationof [0,1,2,...,n-1] named perm isdefinedas:score(perm)=|perm[0]-nums[perm[1]]|+|perm[1]-nums[perm[2]]|+...+|perm[n-1]-......
  • LeetCode 2956. Find Common Elements Between Two Arrays
    原题链接在这里:https://leetcode.com/problems/find-common-elements-between-two-arrays/description/题目:Youaregiventwo 0-indexed integerarrays nums1 and nums2 ofsizes n and m,respectively.Considercalculatingthefollowingvalues:Thenumberof......
  • LeetCode 1915. Number of Wonderful Substrings
    原题链接在这里:https://leetcode.com/problems/number-of-wonderful-substrings/description/题目:A wonderful stringisastringwhere atmostone letterappearsan odd numberoftimes.Forexample, "ccjjc" and "abab" arewonderful,but "ab&......
  • 【LeetCode 875】爱吃香蕉的珂珂
    题目描述原题链接:LeetCode.875爱吃香蕉的珂珂解题思路如果当前堆剩余香蕉数量小于每小时吃的数量,吃完当前堆就会休息不会去吃下一堆的香蕉,所以吃完一堆所需时间就是堆的香蕉数量除以速度的向上取整值:\(\lceil{piles[i]/speed}\rceil\);首先确定答案所处的范围,速度最小......
  • [LeetCode] 1584.连接所有点的最小费用 Kruskal And Prim 算法 Java 并查集
    Problem:1584.连接所有点的最小费用目录Kruskal算法复杂度CodePrim算法复杂度CodeKruskal算法复杂度时间复杂度:添加时间复杂度,示例:$O(mlog(m))$空间复杂度:添加空间复杂度,示例:$O(n)$CodeclassSolution{publicintminCostConnectPoints(int[][]po......
  • LeetCode 3009. Maximum Number of Intersections on the Chart
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart/description/题目:Thereisalinechartconsistingof n pointsconnectedbylinesegments.Youaregivena 1-indexed integerarray y.The kth pointhascoordinates......
  • LeetCode 1287. Element Appearing More Than 25% In Sorted Array
    原题链接在这里:https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/题目:Givenanintegerarray sorted innon-decreasingorder,thereisexactlyoneintegerinthearraythatoccursmorethan25%ofthetime,returnthat......
  • 【LeetCode 410】分割数组的最大值
    题目描述原题链接:LeetCode.410分割数组的最大值解题思路分割的是连续子数组,所以模拟分割求子数组之和可以利用前缀和计算;设前缀和数组preSume[i]含义为[0,..,i]范围元素之和,某个子数组subArray[i,j]的元素和就是preSum[j]-preSum[i];求K个子数组元素和的最大值能转换......
  • 猩球崛起:新世界迅雷BT资源下载[MP4]资源[1080P高清版][HD]
    《猩球崛起:新世界》是宋代文学家张若虚创作的一首抒发对自然景色的赞美和感慨之作,以绮丽的辞藻和深情的笔触描绘了春江的美丽和夜晚的寂静,表达了作者对自然景色的情感倾诉。该诗一经问世,即被广泛传颂,成为中国文学史上的经典之作。本文将以《猩球崛起:新世界》为主题,通过分析......