from utils.treeNode import TreeNode
from utils.treeUtils import generate_tree

class Solution:
    def get_side_view(self, node):
        result = []
        if not node:
            return result
        
        level_nodes = [node]

        while level_nodes:
            result.append(level_nodes[-1].value)
            new_level_nodes = []

            for node in level_nodes:
                if node.left:
                    new_level_nodes.append(node.left)
                
                if node.right:
                    new_level_nodes.append(node.right)

            level_nodes = new_level_nodes
        
        return result
    
    def get_side_view2(self, node):
        result = []
        self.helper(node, 0, result)
        return result
    
    def helper(self, node, level, result):
        if not node:
            return

        if len(result) == level:
            result.append(node.value)
        else:
            result[level] = node.value
        
        self.helper(node.left, level + 1, result)
        self.helper(node.right, level + 1, result)

root = generate_tree()
print(root)
solution = Solution()
print(solution.get_side_view2(root))

