Skip to content

Commit 2ae8bf1

Browse files
committed
333
1 parent 698cbc6 commit 2ae8bf1

File tree

8 files changed

+225
-0
lines changed

8 files changed

+225
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
2+
3+
If two nodes are in the same row and column, the order should be from left to right.
4+
5+
Examples:
6+
7+
Given binary tree [3,9,20,null,null,15,7],
8+
3
9+
/\
10+
/ \
11+
9 20
12+
/\
13+
/ \
14+
15 7
15+
return its vertical order traversal as:
16+
[
17+
[9],
18+
[3,15],
19+
[20],
20+
[7]
21+
]
22+
Given binary tree [3,9,8,4,0,1,7],
23+
3
24+
/\
25+
/ \
26+
9 8
27+
/\ /\
28+
/ \/ \
29+
4 01 7
30+
return its vertical order traversal as:
31+
[
32+
[4],
33+
[9],
34+
[3,0,1],
35+
[8],
36+
[7]
37+
]
38+
Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),
39+
3
40+
/\
41+
/ \
42+
9 8
43+
/\ /\
44+
/ \/ \
45+
4 01 7
46+
/\
47+
/ \
48+
5 2
49+
return its vertical order traversal as:
50+
[
51+
[4],
52+
[9,5],
53+
[3,0,1],
54+
[8,2],
55+
[7]
56+
]
57+
Hide Company Tags Google Facebook
58+
Show Tags
59+
Show Similar Problems
60+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
from collections import defaultdict
9+
import operator
10+
11+
class Solution(object):
12+
def verticalOrder(self, root):
13+
"""
14+
:type root: TreeNode
15+
:rtype: List[List[int]]
16+
"""
17+
if not root:
18+
return []
19+
self.cols = defaultdict(list)
20+
self.recurse(root, 0, 0)
21+
print self.cols
22+
for k in self.cols:
23+
vs = self.cols[k]
24+
self.cols[k] = [v[1] for v in sorted(vs, key = operator.itemgetter(0))]
25+
pairs = sorted(self.cols.items(), key=operator.itemgetter(0))
26+
27+
return [pair[1] for pair in pairs]
28+
29+
def recurse(self, node, n, depth):
30+
# add the node itself into cols
31+
self.cols[n].append((depth,node.val))
32+
if node.left:
33+
self.recurse(node.left, n-1, depth+1)
34+
if node.right:
35+
self.recurse(node.right, n+1, depth+1)
36+
37+
38+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def generateAbbreviations(self, word):
3+
"""
4+
:type word: str
5+
:rtype: List[str]
6+
"""
7+
self.memo = {}
8+
ret =self.recurse(word)
9+
return ret
10+
11+
def recurse(self, word):
12+
if not word:return [""]
13+
if word in self.memo:
14+
return self.memo[word]
15+
results = [word]
16+
for i in range(len(word)):
17+
digits = str(i+1)
18+
for j in range(len(word)-i):
19+
suffixes = self.recurse(word[i+j+2:])
20+
for suffix in suffixes:
21+
if i+j+1<len(word):
22+
results.append(word[:j]+digits+word[i+j+1]+suffix)
23+
else:
24+
results.append(word[:j]+digits)
25+
self.memo[word] = results
26+
return results
27+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Write a function to generate the generalized abbreviations of a word.
2+
3+
Example:
4+
Given word = "word", return the following list (order does not matter):
5+
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
6+
Hide Company Tags Google
7+
Show Tags
8+
Show Similar Problems
9+

325_maximum_size_subarray/max.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def maxSubArrayLen(self, nums, k):
4+
"""
5+
:type nums: List[int]
6+
:type k: int
7+
:rtype: int
8+
"""
9+
sums = defaultdict(list)
10+
sum = 0
11+
result = 0
12+
for i,num in enumerate(nums):
13+
sums[sum].append(i)
14+
sum += num
15+
if sum-k in sums:
16+
result = max(result,i+1-sums[sum-k][0])
17+
18+
return result
19+

325_maximum_size_subarray/problem.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
2+
3+
Example 1:
4+
Given nums = [1, -1, 5, -2, 3], k = 3,
5+
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)
6+
7+
Example 2:
8+
Given nums = [-2, -1, 2, 1], k = 1,
9+
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)
10+
11+
Follow Up:
12+
Can you do it in O(n) time?

333_largest_BST_subtree/largest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 largestBSTSubtree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root: return 0
15+
self.maxsize = 1
16+
self.recurse(root)
17+
return self.maxsize
18+
19+
def recurse(self, node):
20+
# return 3 things:
21+
# 1. whether the subtree rooted at node is BST
22+
# 2. the smallest value in the subtree
23+
# 3. the largest value in the subtree
24+
if not node.left and not node.right:
25+
return (True, node.val, node.val, 1)
26+
27+
is_BST = True
28+
_min = node.val
29+
_max = node.val
30+
size = 1
31+
if node.left:
32+
(is_left_BST, leftmin, leftmax, leftsize) = self.recurse(node.left)
33+
is_BST = is_BST and is_left_BST and node.val>leftmax
34+
_min = min(_min, leftmin)
35+
size += leftsize
36+
if node.right:
37+
(is_right_BST, rightmin, rightmax, rightsize) = self.recurse(node.right)
38+
is_BST = is_BST and is_right_BST and node.val<rightmin
39+
_max = max(_max, rightmax)
40+
size += rightsize
41+
if is_BST and size>self.maxsize:
42+
self.maxsize = size
43+
return is_BST, _min, _max, size
44+

333_largest_BST_subtree/problem.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
3+
4+
Note:
5+
A subtree must include all of its descendants.
6+
Here's an example:
7+
10
8+
/ \
9+
5 15
10+
/ \ \
11+
1 8 7
12+
The Largest BST Subtree in this case is the highlighted one.
13+
The return value is the subtree's size, which is 3.
14+
Hint:
15+
16+
You can recursively use algorithm similar to 98. Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity.

0 commit comments

Comments
 (0)