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] 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].

 vector<vector<int>> findLeaves(TreeNode* root) {
        vector<vector<int>> ret;
        removeLeaves(ret, root);
        return ret;
    }

    int removeLeaves(vector<vector<int>> & ret, TreeNode* root) {
        if (root == NULL) return 0;
        int d1 = removeLeaves(ret, root->left);
        int d2 = removeLeaves(ret, root->right);
        int lev = max(d1, d2) + 1;
        if (ret.size() <= lev) ret.resize(lev);
        ret[lev - 1].push_back(root->val);
        return lev;
    }
时间: 2024-12-20 01:22:08

366. Find Leaves of Binary Tree的相关文章

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

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

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

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

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"