Skip to content

Commit 8d7783b

Browse files
committed
solve problem Convert Sorted Array To Binary Search Tree
1 parent 4550408 commit 8d7783b

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ All solutions will be accepted!
1717
|104|[Maximum Depth Of Binary Tree](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)|[java/py/js](./algorithms/MaximumDepthOfBinaryTree)|Easy|
1818
|617|[Merge Two Binary Trees](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)|[java/py/js](./algorithms/MergeTwoBinaryTrees)|Easy|
1919
|226|[Invert Binary Tree](https://leetcode-cn.com/problems/invert-binary-tree/description/)|[java/py/js](./algorithms/InvertBinaryTree)|Easy|
20-
|693|[Binary Number With Alternating Bits](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)|[java/py/js](./algorithms/BinaryNumberWithAlternatingBits)|Easy|
20+
|693|[Binary Number With Alternating Bits](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)|[java/py/js](./algorithms/BinaryNumberWithAlternatingBits)|Easy|
21+
|108|[Convert Sorted Array To Binary Search Tree](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)|[java/py/js](./algorithms/ConvertSortedArrayToBinarySearchTree)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Convert Sorted Array To Binary Search Tree
2+
Use recursion to solve this problem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode sortedArrayToBST(int[] nums) {
12+
if (nums.length == 0) {
13+
return null;
14+
}
15+
TreeNode root = new TreeNode(nums[nums.length / 2]);
16+
root.left = sortedArrayToBST(Arrays.copyOfRange(nums, 0, nums.length / 2));
17+
root.right = sortedArrayToBST(Arrays.copyOfRange(nums, nums.length / 2 + 1, nums.length));
18+
return root;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {number[]} nums
10+
* @return {TreeNode}
11+
*/
12+
var sortedArrayToBST = function(nums) {
13+
if (nums.length === 0) {
14+
return null
15+
}
16+
let root = new TreeNode(nums[parseInt(nums.length / 2)])
17+
root.left = sortedArrayToBST(nums.slice(0, parseInt(nums.length / 2)))
18+
root.right = sortedArrayToBST(nums.slice(parseInt(nums.length / 2) + 1, nums.length))
19+
return root
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def sortedArrayToBST(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: TreeNode
13+
"""
14+
# for a binary search tree
15+
# this root is middle
16+
if len(nums) == 0:
17+
return None
18+
root = TreeNode(nums[len(nums) / 2])
19+
root.left = self.sortedArrayToBST(nums[0 : (len(nums) / 2)])
20+
root.right = self.sortedArrayToBST(nums[len(nums) / 2 + 1 : len(nums)])
21+
return root

0 commit comments

Comments
 (0)