Find Leaves of Binary Tree

Given a binary tree, collect a tree‘s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:
Given binary tree

          1
         /         2   3
       / \
      4   5

Returns [4, 5, 3], [2], [1].

Explanation:

1. Removing the leaves [4, 5, 3] would result in this tree:

          1
         /
        2

2. Now removing the leaf [2] would result in this tree:

          1

3. Now removing the leaf [1] would result in the empty tree:

          []

Returns [4, 5, 3], [2], [1].

很有意思的一道题,主要是一步一步去除叶子结点,然后返回结果。其实这题考的是二叉树结点的高度。高度最低的先输出,同等高度的一块输出。略微修改下递归球二叉树高度的方法就可以,同时可以根据高度,确定加结果的地方。空间复杂度O(logn),时间复杂度位O(n),每个节点都需要处理一次。代码如下:

class Solution(object):
    def findLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        res = []
        self.dfs(root, res)
        return res
    def dfs(self, root, res):
        if not root:
            return -1
        left = self.dfs(root.left, res)
        right = self.dfs(root.right, res)
        level = max(left, right) + 1
        if len(res) == level:
            res.append([root.val])
        else:
            res[level].append(root.val)
        root.left = root.right = None
        return level
时间: 2024-12-21 03:53:13

Find Leaves of Binary Tree的相关文章

Find Leaves of Binary Tree 找二叉树的叶节点

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation:1. Remove the leaves [4, 5, 3] from the tr

LeetCode Find Leaves of Binary Tree

原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example:Given binary tree 1 / 2 3

366. Find Leaves of Binary Tree C#

Example:Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing the leaves [4, 5, 3] would result in this tree: 1 / 2 2. Now removing the leaf [2] would result in this tree: 1 3. Now removing the leaf [1] would result

366. Find Leaves of Binary Tree

Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example:Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing the leaves [4, 5, 3]

[LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example: Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Remove the leaves [4, 5, 3] from the tr

Leetcode 366. Find Leaves of Binary Tree

Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example: Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing the leaves [4, 5, 3

linked list焦点问题,面经里很多,考虑相交不相交,有环无环 + Find Leaves of Binary Tree (Java)

break the loop at the last node which pointed to the entry. Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example:Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5,

650. Find Leaves of Binary Tree

class Solution { public: vector<vector<int>> findLeaves(TreeNode* root) { vector<vector<int>> res; while (root) { vector<int> leaves; root = remove(root, leaves); res.push_back(leaves); } return res; } TreeNode* remove(TreeNo

655. Print Binary Tree 解题报告(树)

第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / 2 3 4 Output: [["", "", "", "1"