forked from Garvit244/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.
Solution of Remove Invalid Parentheses and Binary Tree Paths
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
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,50 @@ | ||
''' | ||
Given a binary tree, return all root-to-leaf paths. | ||
Note: A leaf is a node with no children. | ||
Example: | ||
Input: | ||
1 | ||
/ \ | ||
2 3 | ||
\ | ||
5 | ||
Output: ["1->2->5", "1->3"] | ||
Explanation: All root-to-leaf paths are: 1->2->5, 1->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 binaryTreePaths(self, root): | ||
""" | ||
:type root: TreeNode | ||
:rtype: List[str] | ||
""" | ||
if not root: | ||
return [] | ||
|
||
paths = [] | ||
def dfs(root, curr): | ||
if root.left is None and root.right is None: | ||
paths.append(curr + str(root.val)) | ||
return | ||
|
||
if root.left: | ||
dfs(root.left, curr + str(root.val) + '->') | ||
if root.right: | ||
dfs(root.right, curr + str(root.val) + '->') | ||
|
||
curr = "" | ||
dfs(root, curr) | ||
return paths |
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,63 @@ | ||
''' | ||
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. | ||
Note: The input string may contain letters other than the parentheses ( and ). | ||
Example 1: | ||
Input: "()())()" | ||
Output: ["()()()", "(())()"] | ||
Example 2: | ||
Input: "(a)())()" | ||
Output: ["(a)()()", "(a())()"] | ||
Example 3: | ||
Input: ")(" | ||
Output: [""] | ||
''' | ||
|
||
class Solution(object): | ||
def removeInvalidParentheses(self, s): | ||
""" | ||
:type s: str | ||
:rtype: List[str] | ||
""" | ||
if not s: | ||
return [""] | ||
|
||
def isValid(s): | ||
count = 0 | ||
for char in s: | ||
if char == '(': | ||
count += 1 | ||
elif char == ')': | ||
count -= 1 | ||
if count < 0: | ||
return False | ||
return (count==0) | ||
|
||
queue, result = [s], [] | ||
visited = set() | ||
visited.add(s) | ||
level = False | ||
|
||
while queue: | ||
new_str = queue.pop(0) | ||
if isValid(new_str): | ||
result.append(new_str) | ||
level = True | ||
|
||
if level: | ||
continue | ||
|
||
for index in range(len(new_str)): | ||
if not (new_str[index] == "(" or new_str[index] == ")"): | ||
continue | ||
partition_str = new_str[0:index] + new_str[index+1:] | ||
if partition_str not in visited: | ||
queue.append(partition_str) | ||
visited.add(partition_str) | ||
return result | ||
|
||
|
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