forked from xcv58/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions for Convert-Sorted-Array-to-Binary-Search-Tree
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <vector> | ||
using namespace std; | ||
|
||
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 sortedArrayToBST(num, 0, num.size()); | ||
} | ||
|
||
TreeNode *sortedArrayToBST(vector<int> &num, int start, int end) { | ||
if (start + 1 >= end) { | ||
return start + 1 == end ? new TreeNode(num[start]) : NULL; | ||
} | ||
|
||
int mid = (start + end) / 2; | ||
TreeNode *root = new TreeNode(num[mid]); | ||
root->left = sortedArrayToBST(num, start, mid); | ||
root->right = sortedArrayToBST(num, mid + 1, end); | ||
|
||
return root; | ||
} | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class Solution { | ||
|
||
public class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
TreeNode(int x) { val = x; } | ||
} | ||
|
||
public TreeNode sortedArrayToBST(int[] num) { | ||
return this.sortedArrayToBST(num, 0, num.length); | ||
} | ||
|
||
public TreeNode sortedArrayToBST(int[] num, int start, int end) { | ||
if (start + 1 >= end) { | ||
return start + 1 == end ? new TreeNode(num[start]) : null; | ||
} | ||
|
||
int mid = (start + end) / 2; | ||
TreeNode root = new TreeNode(num[mid]); | ||
root.left = this.sortedArrayToBST(num, start, mid); | ||
root.right = this.sortedArrayToBST(num, mid + 1, end); | ||
|
||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Definition for a binary tree node | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution: | ||
# @param num, a list of integers | ||
# @return a tree node | ||
def sortedArrayToBST(self, num): | ||
return self.innerSortedArrayToBST(num, 0, len(num)) | ||
|
||
def innerSortedArrayToBST(self, num, start, end): | ||
if start + 1 >= end: | ||
return TreeNode(num[start]) if start + 1 == end else None | ||
|
||
mid = (start + end) / 2 | ||
root = TreeNode(num[mid]) | ||
root.left = self.innerSortedArrayToBST(num, start, mid) | ||
root.right = self.innerSortedArrayToBST(num, mid + 1, end) | ||
|
||
return root |