Leetcode 108
Posted 村雨sup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 108相关的知识,希望对你有一定的参考价值。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { return DFS(nums,0,nums.size()-1); } TreeNode* DFS(vector<int> nums,int left,int right){ if(left > right) return NULL; int mid = (left+right)/2; TreeNode* cur = new TreeNode(nums[mid]); cur->left = DFS(nums,left,mid-1); cur->right = DFS(nums,mid+1,right); return cur; } };
以上是关于Leetcode 108的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode第108题—将有序数组转换为二叉搜索树—Python实现
[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree