forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be65364
commit abdf0fb
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(C) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(C main.cpp) |
75 changes: 75 additions & 0 deletions
75
1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/// Source : https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-06-08 | ||
|
||
#include <iostream> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
using namespace std; | ||
|
||
|
||
/// DFS | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(h) | ||
|
||
/// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
TreeNode* sufficientSubset(TreeNode* root, int limit) { | ||
|
||
unordered_map<TreeNode*, vector<int>> map; | ||
vector<TreeNode*> path; | ||
return dfs(root, 0, path, map, limit); | ||
} | ||
|
||
private: | ||
TreeNode* dfs(TreeNode* node, int cur_sum, vector<TreeNode*>& path, | ||
unordered_map<TreeNode*, vector<int>>& map, int limit){ | ||
|
||
path.push_back(node); | ||
cur_sum += node->val; | ||
|
||
if(!node->left && !node->right){ | ||
for(TreeNode* t: path) | ||
map[t].push_back(cur_sum); | ||
} | ||
else{ | ||
if(node->left) | ||
node->left = dfs(node->left, cur_sum, path, map, limit); | ||
if(node->right) | ||
node->right = dfs(node->right, cur_sum, path, map, limit); | ||
} | ||
|
||
path.pop_back(); | ||
|
||
if(insufficient(map[node], limit)){ | ||
if(node->left && node->right) assert(false); | ||
else if(node->left) return node->left; | ||
else if(node->right) return node->right; | ||
return NULL; | ||
} | ||
return node; | ||
} | ||
|
||
bool insufficient(const vector<int>& vec, int limit){ | ||
|
||
for(int e: vec) | ||
if(e >= limit) return false; | ||
return true; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -737,4 +737,5 @@ email: [[email protected]](mailto:[email protected]) | |
| | | | | | | | ||
| 1078 | [Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/) | [无] | [C++](1078-Occurrences-After-Bigram/cpp-1078/) | | | | ||
| 1079 | [Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/) | [无] | [C++](1079-Letter-Tile-Possibilities/cpp-1079/) | | | | ||
| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [无] | [C++](1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/) | | | | ||
| | | | | | | |