aboutsummaryrefslogtreecommitdiff
path: root/find_largest_val_tree.py
blob: b47375e8a3313ccfae1b349a82290795786d2415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 515. Find Largest Value in Each Tree Row

"""
given the 'root' of a binary tree, return an array of the largest value in
each row of the tree (0-indexed)
"""


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


class Solution(object):
    def largestValues(self, root):
        ans = []
        row = [root]
        while any(row):
            ans.append(max(node.val for node in row))
            row = [child for node in row for child in (node.left, node.right) if child]
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.largestValues(root=[1, 3, 2, 5, 3, null, 9]))  # expect: 1, 3, 9
    print(obj.largestValues(root=[1, 2, 3]))  # expect: 1, 3