Beispiel #1
0
def cloneTree(root: TreeNode) -> TreeNode:
    if root:
        newRoot = TreeNode(root.val)
        newRoot.left = cloneTree(root.left)
        newRoot.right = cloneTree(root.right)
        return newRoot
    return None
 def convertBSTImpl(root: TreeNode, v: int) -> (TreeNode, int):
     if not root:
         return None, 0
     newRight, sumRight = convertBSTImpl(root.right, v)
     newRoot = TreeNode(root.val + sumRight + v)
     newLeft, sumLeft = convertBSTImpl(root.left, newRoot.val)
     newRoot.left = newLeft
     newRoot.right = newRight
     return newRoot, sumLeft + root.val + sumRight
 def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
     if not root:
         return None
     if root.val > high:
         return self.trimBST(root.left, low, high)
     if root.val < low:
         return self.trimBST(root.right, low, high)
     root.left = self.trimBST(root.left, low, high)
     root.right = self.trimBST(root.right, low, high)
     return root
Beispiel #4
0
        def impl(root: TreeNode) -> TreeNode:
            if not root:
                return None
            left_end = impl(root.left)
            right_end = impl(root.right)
            right = root.right
            root.right = root.left
            root.left = None
            if left_end:
                left_end.right = right
            else:
                root.right = right

            if right_end:
                return right_end
            if left_end:
                return left_end

            return root
Beispiel #5
0
 def helper(beg: int, end: int) -> TreeNode:
     if beg > end:
         return None
     mid = beg + (end - beg) // 2
     left = helper(beg, mid - 1)
     root = TreeNode(self.head.val)
     self.head = self.head.next
     root.left = left
     root.right = helper(mid + 1, end)
     return root
Beispiel #6
0
 def impl(pre_b: int, pre_e: int, in_b: int, in_e: int) -> TreeNode:
     if pre_b == pre_e:
         return None
     val = preorder[pre_b]
     root = TreeNode(val)
     m = inorder_index[val]
     left_end = pre_b + 1 + m - in_b
     root.left = impl(pre_b + 1, left_end, in_b, m)
     root.right = impl(left_end, pre_e, m + 1, in_e)
     return root
Beispiel #7
0
Memory Usage: 15.4 MB, less than 98.89% of Python3 online submissions for Path Sum.
"""
class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if root is None:
            return False
        if root.left is None and root.right is None:
            return sum == root.val
        if self.hasPathSum(root.left, sum - root.val):
            return True
        return self.hasPathSum(root.right, sum - root.val)


t1 = TreeNode(5)
t1.left = TreeNode(4)
t1.right = TreeNode(8)

t1.left.left = TreeNode(11)
t1.left.left.left = TreeNode(7)
t1.left.left.right = TreeNode(2)

t1.right.left = TreeNode(13)
t1.right.right = TreeNode(4)

t1.right.right.right = TreeNode(1)

tests = [
    (t1, 22, True)
]

run_functional_tests(Solution().hasPathSum, tests)
# Memory Usage: 18.8 MB, less than 5.07% of Python3 online submissions for Convert BST to Greater Tree.
from Common.Leetcode import TreeNode
from Common.TreeUtils import printTree


class Solution:
    def convertBST(self, root: TreeNode) -> TreeNode:
        def convertBSTImpl(root: TreeNode, v: int) -> (TreeNode, int):
            if not root:
                return None, 0
            newRight, sumRight = convertBSTImpl(root.right, v)
            newRoot = TreeNode(root.val + sumRight + v)
            newLeft, sumLeft = convertBSTImpl(root.left, newRoot.val)
            newRoot.left = newLeft
            newRoot.right = newRight
            return newRoot, sumLeft + root.val + sumRight

        return convertBSTImpl(root, 0)[0]


root1 = TreeNode(4)
root1.left = TreeNode(1)
root1.right = TreeNode(6)
root1.left.left = TreeNode(0)
root1.left.right = TreeNode(2)
root1.left.right.right = TreeNode(3)
root1.right.left = TreeNode(5)
root1.right.right = TreeNode(7)
root1.right.right.right = TreeNode(8)

printTree(Solution().convertBST(root1))
            if L[1] > R[1]:
                return L[0], L[1] + 1
            if R[1] > L[1]:
                return R[0], R[1] + 1
            return root, L[1] + 1
        return dfs(root)[0]


def printTree(root: TreeNode):
    def printTreeImpl(root: TreeNode):
        if root is None:
            return
        printTreeImpl(root.left)
        print(root.val, ' ', end='')
        printTreeImpl(root.right)
    printTreeImpl(root)
    print()


root = TreeNode(3)
root.left = TreeNode(5)
root.left.left = TreeNode(6)
root.left.right = TreeNode(2)
root.left.right.left = TreeNode(7)
root.left.right.right = TreeNode(4)
root.right = TreeNode(1)
root.right.left = TreeNode(0)
root.right.right = TreeNode(8)

res = Solution().lcaDeepestLeaves(root)
printTree(res)
Beispiel #10
0

class Solution:
    @staticmethod
    def isLeave(root: TreeNode):
        return root.left is None and root.right is None

    def sumOfLeftLeavesImpl(self, root: TreeNode, isLeft: bool) -> int:
        if Solution.isLeave(root):
            return root.val if isLeft else 0
        sumleft = sumright = 0
        if root.left:
            sumleft = self.sumOfLeftLeavesImpl(root.left, True)
        if root.right:
            sumright = self.sumOfLeftLeavesImpl(root.right, False)
        return sumleft + sumright

    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if root is None:
            return 0
        return self.sumOfLeftLeavesImpl(root, False)


t1 = TreeNode(3)
t1.left = TreeNode(9)
t1.right = TreeNode(20)
t1.right.left = TreeNode(15)
t1.right.right = TreeNode(7)

print(Solution().sumOfLeftLeaves(t1))  # 24
            if root is None:
                return True, 0
            lb, ll = isBalancedImpl(root.left)
            if not lb:
                return False, ll + 1
            rb, rl = isBalancedImpl(root.right)
            if not rb:
                return False, rl + 1
            return abs(rl - ll) <= 1, max(ll, rl) + 1

        return isBalancedImpl(root)[0]


tree1 = TreeNode(3)
tree1.left = TreeNode(9)
tree1.right = TreeNode(20)
tree1.right.left = TreeNode(15)
tree1.right.right = TreeNode(7)

# print(Solution().isBalanced(tree1))  # True

tree2 = TreeNode(1)
tree2.left = TreeNode(2)
tree2.right = TreeNode(2)

tree2.left.left = TreeNode(3)
tree2.left.right = TreeNode(3)

tree2.left.left.left = TreeNode(4)
tree2.left.left.right = TreeNode(4)
"""


from Common.Leetcode import TreeNode


class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        def isValidBSTImpl(root: TreeNode, vmin, vmax):
            if root is None:
                return True
            if vmin is not None and root.val <= vmin:
                return False
            if vmax is not None and root.val >= vmax:
                return False
            return isValidBSTImpl(root.left, vmin, root.val) and isValidBSTImpl(root.right, root.val, vmax)
        return isValidBSTImpl(root, None, None)


root1 = TreeNode(2)
root1.left = TreeNode(1)
root1.right = TreeNode(3)
print(Solution().isValidBST(root1))  # true

root2 = TreeNode(5)
root2.left = TreeNode(1)
root2.right = TreeNode(4)
root2.right.left = TreeNode(3)
root2.right.right = TreeNode(6)
print(Solution().isValidBST(root2))  # false
            dfs(current.left)
            dfs(current.right)
            if current.val in nodes:
                nodes.remove(current.val)
            else:
                nodes.append(current.val)

        dfs(root)
        return result


root1 = TreeNode(2)
root1.left = TreeNode(3)
root1.left.left = TreeNode(3)
root1.left.right = TreeNode(1)
root1.right = TreeNode(1)
root1.right.right = TreeNode(1)

# if Solution().pseudoPalindromicPaths(root1) == 2:
#     print("PASS")
# else:
#     print("FAIL")

root2 = TreeNode(2)
root2.left = TreeNode(1)
root2.left.left = TreeNode(1)
root2.left.right = TreeNode(3)
root2.left.right.right = TreeNode(1)
root2.right = TreeNode(1)

# if Solution().pseudoPalindromicPaths(root2) == 1:
Beispiel #14
0
class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if p is None and q is None:
            return True
        if p is None or q is None:
            return False
        if p.val != q.val:
            return False
        return self.isSameTree(p.left, q.left) and self.isSameTree(
            p.right, q.right)


tree1 = TreeNode(1)
tree1.left = TreeNode(2)
tree1.right = TreeNode(3)

tree2 = TreeNode(1)
tree2.left = TreeNode(2)
tree2.right = TreeNode(3)

print(Solution().isSameTree(tree1, tree2))  # True

tree1 = TreeNode(1)
tree1.left = TreeNode(2)

tree2 = TreeNode(1)
tree2.right = TreeNode(2)

print(Solution().isSameTree(tree1, tree2))  # False
Beispiel #15
0
class Solution:
    def getTargetCopy(self, original: TreeNode, cloned: TreeNode,
                      target: TreeNode) -> TreeNode:
        if not original:
            return None
        if original is target:
            return cloned
        resultLeft = self.getTargetCopy(original.left, cloned.left, target)
        if resultLeft:
            return resultLeft
        return self.getTargetCopy(original.right, cloned.right, target)


root1 = TreeNode(7)
root1.left = TreeNode(4)
root1.right = TreeNode(3)
root1.right.left = TreeNode(6)
root1.right.right = TreeNode(19)

print(Solution().getTargetCopy(root1, cloneTree(root1), root1.right).val)  # 3

root2 = TreeNode(7)

print(Solution().getTargetCopy(root2, cloneTree(root2), root2).val)  # 7

root3 = TreeNode(8)
root3.right = TreeNode(6)
root3.right.right = TreeNode(5)
root3.right.right.right = TreeNode(4)
root3.right.right.right.right = TreeNode(3)
root3.right.right.right.right.right = TreeNode(2)
    /  \
   15   7
return its depth = 3.
"""
from Common.Leetcode import TreeNode
from Common.ObjectTestingUtils import run_functional_tests
"""
Runtime: 48 ms, faster than 37.70% of Python3 online submissions for Maximum Depth of Binary Tree.
Memory Usage: 15.3 MB, less than 64.04% of Python3 online submissions for Maximum Depth of Binary Tree.
"""


class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root is None:
            return 0

        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1


root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20)

root.right.left = TreeNode(15)
root.right.right = TreeNode(7)

tests = [(root, 3)]

run_functional_tests(Solution().maxDepth, tests)