Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class node{
- const int defaultVal = 0;
- public:
- int val;
- node* left = NULL;
- node* right = NULL;
- node(){
- val = defaultVal;
- }
- node(int value){
- val = value;
- }
- void set(int val, string dir, int index = 0){
- if(index == dir.size()) {
- this->val = val;
- return;
- }
- if(dir[index] == 'L') {
- if(left == NULL)
- left = new node();
- left->set(val, dir, ++index);
- }else {
- if(right == NULL)
- right = new node();
- right->set(val, dir, ++index);
- }
- }
- };
- int main() {
- int numOfNodes, rootVal; cin >> numOfNodes >> rootVal;
- node* root = new node(rootVal);
- for(int i = 0; i < numOfNodes - 1; i++){
- string dir; cin >> dir;
- int val; cin >> val;
- root->set(val, dir);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment