class Solution {
public TreeNode sortedArrayToBST(int[] nums){returnsortedArrayToBST(nums,0, nums.length);}
public TreeNode sortedArrayToBST(int[] nums,int left,int right){if(left >= right){return null;}if(right - left ==1){return new TreeNode(nums[left]);}int mid = left +(right - left)/2;
TreeNode root = new TreeNode(nums[mid]);
root.left =sortedArrayToBST(nums, left, mid);
root.right =sortedArrayToBST(nums, mid +1, right);return root;}}
三、538.把二叉搜索树转换为累加树
class Solution {int sum;
public TreeNode convertBST(TreeNode root){
sum =0;convertBST1(root);return root;}// 按右中左顺序遍历,累加即可
public voidconvertBST1(TreeNode root){if(root == null){return;}convertBST1(root.right);
sum += root.val;
root.val = sum;convertBST1(root.left);}}