-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108. Convert Sorted Array to Binary Search Tree (Divide and Conquer) 20.3.18 Easy
More file actions
89 lines (78 loc) · 3.58 KB
/
108. Convert Sorted Array to Binary Search Tree (Divide and Conquer) 20.3.18 Easy
File metadata and controls
89 lines (78 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of
every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
Solution: -------------------------------------------------- Two Pointers + DFS
-------------------------------------- O(N) T, O(logN) S
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sortedArrayToBST(self, nums):
return self.to_bst_help(nums, 0, len(nums)-1)
"""
:type nums: List[int]
:rtype: TreeNode # It requires us to return a node
"""
def to_bst_help(self, nums, start, end):
if start > end: # This condition used very often, need to be remembered
return None # "return None" or "return" are all OK !!!!!!!!!!!!!!!!!!!!!!!!
mid = (start + end) / 2 # '/' in python 2 is '//'
# Even though Python does not overflow,
# it is still better to change mid = (start + end) / 2
# into mid = start + (end - start) / 2 and explain it to the interviewers
node = TreeNode(nums[mid]) # Can define a node using its build-in class
node.left = self.to_bst_help(nums, start, mid - 1) # Since the function need to return a node, we should use assignment here
node.right = self.to_bst_help(nums, mid + 1, end)
'''
node.left = nums[mid - 1] # Why cannot do like this?
# mid may be zero e.x.: (0 + 1)/ 2 = 0
# and node.left should be None in the end as in the line 34
# So, use the recursion function for assignment
# THINK LIKE A PROGRAMMER!
node.right = nums[end]
self.to_bst_help(nums, start, mid - 1)
self.to_bst_help(nums, mid + 1, end)
'''
return node # As the question required
Java Version:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
return build(nums, 0, nums.length - 1);
}
public TreeNode build(int[] nums, int left, int right) {
if (left > right) {
return null;
}
if (left == right) {
return new TreeNode(nums[left]);
}
int mid = left + (right - left) / 2;
TreeNode currNode = new TreeNode(nums[mid]);
currNode.left = build(nums, left, mid - 1);
currNode.right = build(nums, mid + 1, right);
return currNode;
}
}