-
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.
- Loading branch information
1 parent
99377b1
commit 8451523
Showing
13 changed files
with
359 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,18 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
val: int = 0 | ||
|
||
def bstToGst(self, root: TreeNode) -> TreeNode: | ||
|
||
if root: | ||
self.bstToGst(root.right) | ||
self.val += root.val | ||
root.val = self.val | ||
self.bstToGst(root.left) | ||
|
||
return root |
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,34 @@ | ||
# Definition for a binary tree node. | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
|
||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
|
||
class Solution: | ||
def maxDepth(self, root: TreeNode) -> int: | ||
if not root: | ||
return 0 | ||
|
||
q = collections.deque([root]) | ||
depth = 0 | ||
while q: | ||
depth += 1 | ||
|
||
for _ in range(len(q)): | ||
cur_root = q.popleft() | ||
if cur_root.left: | ||
q.append(cur_root.left) | ||
|
||
if cur_root.right: | ||
q.append(cur_root.right) | ||
|
||
|
||
|
||
return depth | ||
|
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,21 @@ | ||
# Definition for a binary tree node. | ||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
class Solution: | ||
def sortedArrayToBST(self, nums: List[int]) -> TreeNode: | ||
|
||
if not nums: | ||
return None | ||
|
||
mid = len(nums) // 2 | ||
|
||
node = TreeNode(nums[mid]) | ||
node.left = self.sortedArrayToBST(nums[:mid]) | ||
node.right = self.sortedArrayToBST(nums[mid+1:]) | ||
|
||
return node |
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,47 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
# my solve | ||
class Solution: | ||
diff : int = 0 | ||
def isBalanced(self, root: TreeNode) -> bool: | ||
if not root: | ||
return True | ||
|
||
|
||
def dfs(node: TreeNode) -> int: | ||
|
||
if not node: | ||
return 0 | ||
|
||
left = dfs(node.left) | ||
right = dfs(node.right) | ||
|
||
height = max(left, right) | ||
self.diff = max(self.diff, abs(left-right)) | ||
|
||
return height + 1 | ||
|
||
dfs(root) | ||
return True if self.diff < 2 else False | ||
|
||
# another solve | ||
class Solution2: | ||
def isBalanced(self, root: TreeNode) -> bool: | ||
def dfs(node: TreeNode) -> int: | ||
|
||
if not node: | ||
return 0 | ||
|
||
left = dfs(node.left) | ||
right = dfs(node.right) | ||
|
||
if left == -1 or right == -1 or abs(left-right) > 1: | ||
return -1 | ||
|
||
return max(left, right) + 1 | ||
|
||
return dfs(root) != -1 |
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,19 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def invertTree(self, root: TreeNode) -> TreeNode: | ||
if root: | ||
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) | ||
return root | ||
return None | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
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,62 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
# Definition for a binary tree node. | ||
|
||
|
||
class TreeNode(object): | ||
def __init__(self, x): | ||
self.val = x | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
class Codec: | ||
|
||
def serialize(self, root): | ||
queue = collections.deque([root]) | ||
|
||
result = ['#'] | ||
while queue: | ||
node = queue.popleft() | ||
if node: | ||
queue.append(node.left) | ||
queue.append(node.right) | ||
|
||
result.append(str(node.val)) | ||
|
||
else: | ||
result.append('#') | ||
|
||
return ' '.join(result) | ||
|
||
def deserialize(self, data): | ||
if data == '# #': | ||
return None | ||
|
||
nodes = data.split() | ||
|
||
root = TreeNode(int(nodes[1])) | ||
queue = collections.deque([root]) | ||
|
||
index = 2 | ||
|
||
while queue: | ||
node = queue.popleft() | ||
if nodes[index] is not '#': | ||
node.left = TreeNode(int(nodes[index])) | ||
queue.append(node.left) | ||
index += 1 | ||
|
||
if nodes[index] is not '#': | ||
node.right = TreeNode(int(nodes[index])) | ||
queue.append(node.right) | ||
index += 1 | ||
return root | ||
|
||
|
||
# Your Codec object will be instantiated and called as such: | ||
# ser = Codec() | ||
# deser = Codec() | ||
# ans = deser.deserialize(ser.serialize(root)) |
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,33 @@ | ||
class Solution: | ||
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]: | ||
if n <= 1: | ||
return [0] | ||
|
||
graph = collections.defaultdict(list) | ||
|
||
for i, j in edges: | ||
graph[i].append(j) | ||
graph[j].append(i) | ||
|
||
|
||
leaves = [] | ||
|
||
for key in graph.keys(): | ||
if len(graph[key]) == 1: | ||
leaves.append(key) | ||
|
||
|
||
|
||
while n > 2: | ||
n -= len(leaves) | ||
new_leaves = [] | ||
for leaf in leaves: | ||
neighbor = graph[leaf].pop() | ||
graph[neighbor].remove(leaf) | ||
|
||
if len(graph[neighbor]) == 1: | ||
new_leaves.append(neighbor) | ||
|
||
leaves = new_leaves | ||
|
||
return leaves |
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,26 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
longest: int = 0 | ||
def diameterOfBinaryTree(self, root: TreeNode) -> int: | ||
|
||
|
||
def dfs(node: TreeNode) -> int: | ||
|
||
if not node: | ||
return -1 | ||
|
||
left = dfs(node.left) | ||
right = dfs(node.right) | ||
|
||
self.longest = max(self.longest, left + right + 2) | ||
|
||
return max(left, right) + 1 | ||
|
||
dfs(root) | ||
|
||
return self.longest |
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,19 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: | ||
if root1 and root2: | ||
root1.val = root1.val + root2.val | ||
root1.left = self.mergeTrees(root1.left, root2.left) | ||
root1.right = self.mergeTrees(root1.right, root2.right) | ||
|
||
return root1 | ||
|
||
else: | ||
return root1 or root2 | ||
|
||
|
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,34 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
result: int = 0 | ||
|
||
def longestUnivaluePath(self, root: TreeNode) -> int: | ||
|
||
def dfs(node: TreeNode) -> int: | ||
if node is None: | ||
return 0 | ||
|
||
left = dfs(node.left) | ||
right = dfs(node.right) | ||
|
||
if node.left and node.val == node.left.val: | ||
left += 1 | ||
else: | ||
left = 0 | ||
|
||
if node.right and node.val == node.right.val: | ||
right += 1 | ||
else: | ||
right = 0 | ||
|
||
self.result = max(self.result, left + right) | ||
|
||
return max(left, right) | ||
|
||
dfs(root) | ||
return self.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
prev = -sys.maxsize | ||
result = sys.maxsize | ||
def minDiffInBST(self, root: TreeNode) -> int: | ||
|
||
if root.left: | ||
self.minDiffInBST(root.left) | ||
|
||
self.result = min(self.result, root.val - self.prev) | ||
self.prev = root.val | ||
|
||
if root.right: | ||
self.minDiffInBST(root.right) | ||
|
||
return self.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
result: int = 0 | ||
|
||
def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int: | ||
|
||
if root: | ||
if root.val in range(low, high+1): | ||
self.result += root.val | ||
|
||
if root.val > high: | ||
self.rangeSumBST(root.left, low, high) | ||
elif root.val < low: | ||
self.rangeSumBST(root.right, low, high) | ||
else: | ||
self.rangeSumBST(root.left, low, high) | ||
self.rangeSumBST(root.right, low, high) | ||
|
||
return self.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