LeetCode Convert Sorted Array to Binary Search Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *sortedArrayToBST(vector<int> &num) { return dfs(0, num.size(), num); } TreeNode* dfs(int start, int end, vector<int>& num) { if (start >= end) return NULL; int mid = ( start + end ) / 2; TreeNode* nroot = new TreeNode(num[mid]); nroot->left = dfs(start, mid, num); nroot->right= dfs(mid + 1, end, num); return nroot; } };
假期回来,水一发
第二轮:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
写出来一样的代码