#     def __init__(self, val = 0, left = None, right = None):
#         self.val = val
#         self.left = left
#         self.right = right

from typing import List
from util import TreeNode, initTree


class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        ans = []
        Q = [(root, 0)]
        while Q:
            node, level = Q.pop(0)
            if level < len(ans):
                ans[len(ans) - level - 1].append(node.val)
            else:
                ans = [[node.val]] + ans
            if node.left != None:
                Q.append((node.left, level + 1))
            if node.right != None:
                Q.append((node.right, level + 1))
        return ans


tree = initTree([3, 9, 20, None, None, 15, 7])
print(Solution().levelOrderBottom(tree))  # [[15, 7], [9, 20], [3]]
Ejemplo n.º 2
0
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

import sys
sys.path.append('../')

from util import TreeNode, initTree


class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0
        Q, left_leaves = [(root, False)], []
        while Q:
            node, isLeft = Q.pop(0)
            left_leaves += [
                node.val
            ] if not node.left and not node.right and isLeft else []
            Q += [(node.left, True)] if node.left else []
            Q += [(node.right, False)] if node.right else []
        return sum(left_leaves)


print(Solution().sumOfLeftLeaves(initTree([3, 9, 20, None, None, 15,
                                           7])))  # 24
print(Solution().sumOfLeftLeaves(initTree([3])))  # 0
Ejemplo n.º 3
0
            if not tree:
                return 0
            cummulative_sum += tree.val
            ans = counter[cummulative_sum - sum]
            counter[cummulative_sum] += 1
            ans += traverse(tree.left, counter, cummulative_sum) + \
                traverse(tree.right, counter, cummulative_sum)
            counter[cummulative_sum] -= 1
            return ans

        return traverse(root, counter)


# # Testcases
# [10,5,-3,3,2,null,11,3,-2,null,1]
# 8
# [1,null,2,null,3,null,4,null,5]
# 3
# [1,-2,-3,1,3,-2,null,-1]
# 3

# 3
print(Solution().pathSum(initTree([10, 5, -3, 3, 2, None, 11, 3, -2, None, 1]),
                         8))

# 2
print(Solution().pathSum(initTree([1, None, 2, None, 3, None, 4, None, 5]), 3))

# 1
print(Solution().pathSum(initTree([1, -2, -3, 1, 3, -2, None, -1]), 3))
Ejemplo n.º 4
0
"""

# # 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

from util import TreeNode, initTree

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if p == None and q == None:
            return True
        elif not p or not q:
            return False
        return p.val == q.val and \
            self.isSameTree(p.left, q.left) and \
            self.isSameTree(p.right, q.right)

p, q = initTree([1, 2, 3]), initTree([1, 2, 3])
print(Solution().isSameTree(p, q))  # True

p, q = initTree([1, 2]), initTree([1, None, 2])
print(Solution().isSameTree(p, q))  # False

p, q = initTree([1, 2, 1]), initTree([1, 1, 2])
print(Solution().isSameTree(p, q))  # False

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def widthOfBinaryTree(self, root: TreeNode) -> int:
        if not root:
            return 0
        ans, Q, curr_idx, curr_level = 0, [(0, 0, root)], 0, 0
        while Q:
            idx, level, node = Q.pop(0)
            if level == curr_level:
                ans = max(ans, idx - curr_idx + 1)
            else:
                curr_idx, curr_level = idx, level
            Q += [(idx * 2 + 1, level + 1, node.left)] if node.left else []
            Q += [(idx * 2 + 2, level + 1, node.right)] if node.right else []
        return ans


print(Solution().widthOfBinaryTree(initTree([1, 3, 2, 5, 3, None, 9])))  # 4
print(Solution().widthOfBinaryTree(initTree([1, 3, None, 5, 3])))  # 2
print(Solution().widthOfBinaryTree(initTree([1, 3, None, 5, 3])))  # 2
print(Solution().widthOfBinaryTree(
    initTree(
        [1, 3, 2, 5, None, None, 9, 6, None, None, None, None, None, None,
         7])))  # 8
                return
            if tree.left:
                traverse(tree.left, lst)
            lst.append(tree.val)
            if tree.right:
                traverse(tree.right, lst)

        # Traverse both trees
        traverse(root1, lst1)
        traverse(root2, lst2)

        # Merge
        while lst1 and lst2:
            lst += [lst1.pop(0)] if lst1[0] < lst2[0] else [lst2.pop(0)]

        return lst + lst1 + lst2


# [0, 1, 1, 2, 3, 4]
print(Solution().getAllElements(initTree([2, 1, 4]), initTree([1, 0, 3])))

# [-10, 0, 0, 1, 2, 5, 7, 10]
print(Solution().getAllElements(initTree([0, -10, 10]),
                                initTree([5, 1, 7, 0, 2])))

# [0, 1, 2, 5, 7]
print(Solution().getAllElements(initTree([]), initTree([5, 1, 7, 0, 2])))

# [-10, 0, 10]
print(Solution().getAllElements(initTree([0, -10, 10]), initTree([])))
                map(lambda lst: sorted(lst), left + right)))


# # Testcases
# [3,9,20,null,null,15,7]
# [1,2,3,4,5,6,7]
# [0,null,1,2,3,null,null,4,5]
# [0,8,1,null,null,3,2,null,4,5,null,null,7,6]
# [0,1,null,null,2,6,3,null,null,null,4,null,5]
# [0,2,1,3,null,null,null,4,5,null,7,6,null,10,8,11,9]

# # ANS
# [[9],[3,15],[20],[7]]
# [[4],[2],[1,5,6],[3],[7]]
# [[0,2],[1,4],[3],[5]]
# [[8],[0,3,6],[1,4,5],[2,7]]
# [[1,6],[0,2],[3],[4],[5]]
# [[4,10,11],[3,6,7],[2,5,8,9],[0],[1]]

print(Solution().verticalTraversal(initTree([3, 9, 20, None, None, 15, 7])))
print(Solution().verticalTraversal(initTree([1, 2, 3, 4, 5, 6, 7])))
print(Solution().verticalTraversal(
    initTree([0, None, 1, 2, 3, None, None, 4, 5])))
print(Solution().verticalTraversal(
    initTree([0, 8, 1, None, None, 3, 2, None, 4, 5, None, None, 7, 6])))
print(Solution().verticalTraversal(
    initTree([0, 1, None, None, 2, 6, 3, None, None, None, 4, None, 5])))
print(Solution().verticalTraversal(
    initTree(
        [0, 2, 1, 3, None, None, None, 4, 5, None, 7, 6, None, 10, 8, 11, 9])))
# 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 zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        queue, ans = [(root, 0)], []
        while queue:
            node, level = queue.pop(0)
            if len(ans) > level:
                if level % 2 == 0:  # left to right
                    ans[level].append(node.val)
                else:  # right to left
                    ans[level] = [node.val] + ans[level]
            else:  # New level
                ans.append([node.val])
            queue += [(node.left, level + 1)] if node.left else []
            queue += [(node.right, level + 1)] if node.right else []
        return ans


# [[3], [20, 9], [15, 7]]
print(Solution().zigzagLevelOrder(initTree([3, 9, 20, None, None, 15, 7])))