222222/leetcode

/154_Maximum_Depth_of_Binary_Tree.py
# 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 diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0

def deep(root):
if not root:
return 0
else:
return max(deep(root.left), deep(root.right)) + 1

return max(deep(root.left) + deep(root.right), self.diameterOfBinaryTree(root.left), self.diameterOfBinaryTree(root.right))

/653_Maximum_Level_Sum_of_Binary_Tree.py
# 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 minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""

if not root:
return 0

if not root.left:
return self.minDepth(root.right) + 1
if not root.right:
return self.minDepth(root.left) + 1
else:
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

/109_Convert_Sorted_List_to_Binary_Tree.py
# 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 minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""

if not root:
return 0

if not root.left and not root.right:
return 1

left_depth = self.minDepth(root.left) if root.left else float("inf")
right_depth = self.minDepth(root.right) if root.right else float("inf")

return min(left_depth, right_depth) + 1

/104_Maximum_Depth_of_Binary_Tree.py
# 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 lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""

if not root:
return None

if root == p or root == q:
return root

left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)

if left and right:
return root

if left:
return left

if right:
return right

/149_Maximum_Decreasing_Subsequence_Length.py
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head

def merge(head1, head2):
if not head1 or not head2:
return head1 if head1 else head2

if head1.val < head2.val:
head1.next = merge(head1.next, head2)
return head1
else:
head2.next = merge(head1, head2.next)
return head2

def partition(head):
if not head:
return head
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow

mid = partition(head)
head1 = head
head2 = mid.next
mid.next = None

return merge(self.sortList(head1), self.sortList(head2))

/107_Binary_Tree_Level_Order_Traversal.py
# 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 buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""

def build(preorder, inorder):
if not preorder:
return None
root = TreeNode(preorder[0])
mid = inorder.index(preorder[0])
root.left = build(preorder[1:mid+1], inorder[:mid])
root.right = build(preorder[mid+1:], inorder[mid+1:])
return root

return build(preorder, inorder)

/94_Inorder_Successor_in_Sorted_Binary_Tree.py
# 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 levelOrderBottom(self, root):
"""