-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,61 @@ | ||
''' | ||
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n. | ||
Example: | ||
Input: 3 | ||
Output: | ||
[ | ||
[1,null,3,2], | ||
[3,2,null,1], | ||
[3,1,null,null,2], | ||
[2,1,3], | ||
[1,null,2,null,3] | ||
] | ||
Explanation: | ||
The above output corresponds to the 5 unique BST's shown below: | ||
1 3 3 2 1 | ||
\ / / / \ \ | ||
3 2 1 1 3 2 | ||
/ / \ \ | ||
2 1 2 3 | ||
''' | ||
|
||
# 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 generateTrees(self, n): | ||
""" | ||
:type n: int | ||
:rtype: List[TreeNode] | ||
""" | ||
if n == 0: | ||
return [] | ||
|
||
|
||
def generate(start, end): | ||
result = [] | ||
if start > end: | ||
result.append(None) | ||
return result | ||
|
||
for index in range(start, end+1): | ||
left = generate(start, index-1) | ||
right = generate(index+1, end) | ||
|
||
for l in left: | ||
for r in right: | ||
current = TreeNode(index) | ||
current.left = l | ||
current.right = r | ||
result.append(current) | ||
|
||
return result | ||
|
||
return generate(1, n) |
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,41 @@ | ||
''' | ||
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. | ||
Example 1: | ||
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" | ||
Output: true | ||
Example 2: | ||
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" | ||
Output: false | ||
''' | ||
|
||
class Solution(object): | ||
def isInterleave(self, s1, s2, s3): | ||
""" | ||
:type s1: str | ||
:type s2: str | ||
:type s3: str | ||
:rtype: bool | ||
""" | ||
|
||
if len(s3) != len(s1) + len(s2): | ||
return False | ||
|
||
dp = [[False for _ in range(len(s2)+1)] for _ in range(len(s1)+1)] | ||
for row in range(len(s1)+1): | ||
for col in range(len(s2)+1): | ||
if row == 0 and col == 0: | ||
dp[row][col] = True | ||
elif row == 0: | ||
dp[row][col] =dp[row][col-1] and s2[col-1] == s3[row+col-1] | ||
elif col == 0: | ||
dp[row][col] = dp[row-1][col] and s1[row-1] == s3[row+col-1] | ||
else: | ||
dp[row][col] = (dp[row][col-1] and s2[col-1] == s3[row+col-1]) or (dp[row-1][col] and s1[row-1] == s3[row+col-1]) | ||
|
||
return dp[len(s1)][len(s2)] | ||
|
||
# Time: O(m*n) | ||
# Space: O(m*n) |
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,42 @@ | ||
''' | ||
Given a binary tree, determine if it is a valid binary search tree (BST). | ||
Assume a BST is defined as follows: | ||
The left subtree of a node contains only nodes with keys less than the node's key. | ||
The right subtree of a node contains only nodes with keys greater than the node's key. | ||
Both the left and right subtrees must also be binary search trees. | ||
''' | ||
|
||
# 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 isValidBST(self, root): | ||
""" | ||
:type root: TreeNode | ||
:rtype: bool | ||
""" | ||
if not root: | ||
return True | ||
|
||
stack, result = [], [] | ||
while stack or root: | ||
if root: | ||
stack.append(root) | ||
root = root.left | ||
else: | ||
root = stack.pop() | ||
result.append(root.val) | ||
root = root.right | ||
|
||
previous = result[0] | ||
for index in range(1, len(result)): | ||
if previous >= result[index]: | ||
return False | ||
previous = result[index] | ||
return True |
File renamed without changes.
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,45 @@ | ||
''' | ||
Given two binary trees, write a function to check if they are the same or not. | ||
Two binary trees are considered the same if they are structurally identical and the nodes have the same value. | ||
Example 1: | ||
Input: 1 1 | ||
/ \ / \ | ||
2 3 2 3 | ||
[1,2,3], [1,2,3] | ||
Output: true | ||
''' | ||
|
||
# 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 isSameTree(self, p, q): | ||
""" | ||
:type p: TreeNode | ||
:type q: TreeNode | ||
:rtype: bool | ||
""" | ||
if not p and not q: | ||
return True | ||
|
||
stack = [(p, q)] | ||
|
||
while stack: | ||
node1, node2 = stack.pop() | ||
if node1 and node2 and node1.val == node2.val: | ||
stack.append((node1.left, node2.left)) | ||
stack.append((node1.right, node2.right)) | ||
else: | ||
if not node1 == node2: | ||
return False | ||
|
||
return True |