LeetCode 366 Find Leaves of Binary Tree

LeetCode 366 Find Leaves of Binary Tree Problem

Download Code
# 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 findLeaves(self, root):
    #     """
    #     :type root: TreeNode
    #     :rtype: List[List[int]]
    #     """
    #     res = []
    #     if root is None:
    #         return []
    #     stack = [root]
    #     check_set = set()
    #     while len(stack) > 0:
    #         curr = stack.pop()
    #         check_set.add(curr)
    #         if curr.left:
    #             stack.append(curr.left)
    #         if curr.right:
    #             stack.append(curr.right)
    #     while len(check_set) > 0:
    #         curr = []
    #         for node in check_set:
    #             if (node.left is None or node.left not in check_set) and\
    #                (node.right is None or node.right not in check_set):
    #                 curr.append(node)
    #         res.append([node.val for node in curr])
    #         for node in curr:
    #             check_set.remove(node)
    #     return res

    def findLeaves(self, root):
        res = []
        self.findLeaves_helper(root, res)
        return res

    def findLeaves_helper(self, node, res):
        if node is None:
            return -1
        level = 1 + max(self.findLeaves_helper(node.left, res), self.findLeaves_helper(node.right, res))
        if len(res) < level + 1:
            res.append([])
        res[level].append(node.val)
        return level
Download Find Leaves of Binary Tree.py

List of all Find Leaves of Binary Tree problems

Leetcode 366 Find Leaves of Binary Tree problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

self.val = x

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.