Created
June 18, 2019 10:00
-
-
Save johnjohnlin/bbc99bcf4c0c78086f407ec2b06e9379 to your computer and use it in GitHub Desktop.
A snippet that can help you make TreeNode structure from Leetcode tree text like [1,2,-3,-5,null,4,null].
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
#include <algorithm> | |
#include <utility> | |
#include <stack> | |
#include <vector> | |
using namespace std; | |
#define null nullptr | |
struct TreeNode { | |
int val; | |
TreeNode *left; | |
TreeNode *right; | |
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
}; | |
struct _TreeNode { | |
TreeNode *n; | |
_TreeNode(nullptr_t): n(nullptr) {} | |
_TreeNode(int i): n(new TreeNode(i)) {} | |
}; | |
TreeNode* make_leetcode_tree(const std::vector<_TreeNode> &v) | |
{ | |
for (size_t i = 1; i < v.size(); i++) { | |
if (v[i].n && v[(i-1)/2].n) { | |
if (i%2 == 0) v[(i-1)/2].n->right = v[i].n; | |
else v[(i-1)/2].n->left = v[i].n; | |
} | |
} | |
return v.front().n; | |
} | |
/////////////////////////// | |
// Example: Leetcode 1080 | |
/////////////////////////// | |
class Solution { | |
int cur_; | |
int limit_; | |
int Traverse(TreeNode *father, TreeNode *p) { | |
cur_ += p->val; | |
int max_val = 255<<24; | |
if (p->left) { | |
max_val = max(max_val, Traverse(p, p->left)); | |
} | |
if (p->right) { | |
max_val = max(max_val, Traverse(p, p->right)); | |
} | |
int ret = p->val; | |
if (p->left or p->right) { | |
ret += max_val; | |
} | |
cur_ -= p->val; | |
if (cur_ + ret < limit_ && father) { | |
if (father->left == p) father->left = NULL; | |
if (father->right == p) father->right = NULL; | |
} | |
return ret; | |
} | |
public: | |
TreeNode* sufficientSubset(TreeNode* root, int limit) { | |
TreeNode fake(0); | |
limit_ = limit; | |
fake.left = root; | |
Traverse(nullptr, &fake); | |
return fake.left; | |
} | |
}; | |
int main() | |
{ | |
Solution s; | |
TreeNode *ans[] = { | |
s.sufficientSubset(make_leetcode_tree({1,2,-3,-5,null,4,null}), -1), | |
s.sufficientSubset(make_leetcode_tree({1,2,3,4,null,null,7,8,9,null,14}), 1), | |
s.sufficientSubset(make_leetcode_tree({19116,-6419,-6972,-242,-942,11391,-3065,2811,-8816,-176,-3984,null,14568,18522,16054,null,-1050,-216,-3729,3064,-1069,9215,-3891,18451,null,10794,9707,3745,-8632,7591,11919,-1590,7734,5023,null,19776,-4844,6683,null,-8944,5188,null,11648,5969,null,null,null,null,null,18971,10446,385,-6543,null,15641,4466,-1091,null,null,null,9520,null,null,null,null,6182,103,3298,8811,10086,10004,-3613,13611,-7252,9553,null,null,null,11078,-6612,-551,null,null,6704,null,null,null,-7719,-6508,5648,3637,null,null,-6988,null,null,-7918,null,-4853,-3715,null,null,null,-8433,null,null,null,null,null,null,-4601,null,null,null,null,-2396,-5951,null,null,6509,9169,19019,11715,3478,null,5448,null,null,12673,null,null,null,null,null,5561,null,null,6088,null,null,17861,10581,18042,null,null,null,null,null,null,4163,15418,null,null,null,null,null,10111,null,-8427,null,12522,-1817,null,null,null,null,null,-1818,null,-579,7729,null,null,null,null,null,null,null,11712,2441,8892,null,null,null,null,7849,null,null,null,null,436,-7846,null,null,null,null,null,6982}), 801432710) | |
}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment