366. Find Leaves of Binary Tree
Given the root of a binary tree, collect a tree’s nodes as if you were doing this:
- Collect all the leaf nodes.
- Remove all the leaf nodes.
- Repeat until the tree is empty.
- new solution update 2022.05.13
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
|
class Solution { vector<vector<int>> res; int helper(TreeNode* node) { if(!node) return -1; int lvl = max(helper(node->left) + 1, helper(node->right) + 1); if(res.size() == lvl) res.emplace_back(); res[lvl].push_back(node->val); return lvl; } public: vector<vector<int>> findLeaves(TreeNode* root) { helper(root); return res; } };
|
- dfs solution
- Time : O(n)
- Space : O(1)
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 30 31 32 33 34 35 36 37 38 39 40
|
class Solution { vector<vector<int>> res; bool isLeaf(TreeNode* node) { return node->left == nullptr and node->right == nullptr; } int travel(TreeNode* node) { if(!node) return -1; if(isLeaf(node)) { if(res.empty()) res.push_back(vector<int>()); res[0].push_back(node->val); return 0; } int level = max(travel(node->left), travel(node->right)); level++; while(res.size() <= level) { res.push_back(vector<int>()); } res[level].push_back(node->val); return level; } public: vector<vector<int>> findLeaves(TreeNode* root) { travel(root); return res; } };
|
- topology sort solution
- Time : O(n)
- Space : O(n)
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
class Solution { vector<TreeNode*> leaf; unordered_map<TreeNode*, TreeNode*> parent; bool isLeaf(TreeNode* node) { return !node->left and !node->right; } void search(TreeNode* node) { if(isLeaf(node)) { leaf.push_back(node); } if(node->left) { parent[node->left] = node; search(node->left); } if(node->right) { parent[node->right] = node; search(node->right); } } TreeNode* getUnLinkParent(TreeNode* node) { TreeNode* p = parent[node]; if(p == NULL) return NULL; if(p->left == node) p->left = NULL; else p->right = NULL; return p; } public: vector<vector<int>> findLeaves(TreeNode* root) { search(root); vector<vector<int>> res; while(!leaf.empty()) { vector<int> ans; vector<TreeNode*> nxt; for(auto& node : leaf) { ans.push_back(node->val); auto parent = getUnLinkParent(node); if(parent && isLeaf(parent)) { nxt.push_back(parent); } } nxt.swap(leaf); res.push_back(ans); } return res; } };
|
- dfs solution
- Time : O(nh)
- Space : O(1)
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 30 31 32 33 34 35 36 37
|
class Solution { vector<vector<int>> res; bool isLeafNode(TreeNode* node) { if(!node->left and !node->right){ res.back().push_back(node->val); return true; }
if(node->left and isLeafNode(node->left)) { node->left = NULL; } if(node->right and isLeafNode(node->right)) { node->right = NULL; } return false; } public: vector<vector<int>> findLeaves(TreeNode* root) { res.push_back(vector<int>()); while(!isLeafNode(root)) { res.push_back(vector<int>()); } return res; } };
|