Skip to content

Commit

Permalink
1080 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jun 16, 2019
1 parent be65364 commit abdf0fb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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 1080-Insufficient-Nodes-in-Root-to-Leaf-Paths/cpp-1080/main.cpp
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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | | |
| | | | | | |

0 comments on commit abdf0fb

Please sign in to comment.