int main() { Solution sol; std::vector<int> data = {0,1,2,3,4,5,6}; auto root = constructTreeByLevel(data); prettyPrintTree(root); auto res = sol.rightSideView(root); std::cout << "res: "; std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, ", ")); std::cout << std::endl; return 0; }
void prettyPrintTree(NodeT *root,int recLevel) //! root, index, length, reccurence level { if(root==NULL) { recLevel--; //! reached leaf, must decrement recurence level return; } recLevel++; //! otherwise increment it prettyPrintTree(root->right,recLevel); //! keep going right in the tree int j=0; //! print spaces for the appropriate recurence level for(j=0; j<recLevel-1; j++) { printf(" "); } //! then print value printf("%s", root->data); //! print a new line printf("\n"); prettyPrintTree(root->left,recLevel); //! keep going left in the tree }
int main() { Solution sol; std::vector<std::pair<std::vector<int>, std::vector<int>>> tests = { { {2,1,3}, {2,3,1} } , { {3,2,1}, {3,2,1} } , { {1,2,3}, {3,2,1} } }; for (auto& test : tests) { std::cout << "in order: "; std::copy(test.first.begin(), test.first.end(), std::ostream_iterator<int>(std::cout, ", ")); std::cout << std::endl; auto res = sol.buildTree(test.first, test.second); prettyPrintTree(res); std::cout << std::endl; } return 0; }
int main() { NodeQ* head=NULL; NodeT* root=NULL; FILE* f=fopen("input.txt", "r"); if(f==0) printf("err"); char* s=(char*)malloc(sizeof(char)*100); fscanf(f, "%[^\n]s", s); printf("%s\n", s); //create queue: char* token=strtok(s, " "); while(token!=NULL) { enqueue(&head, token); token=strtok(NULL, " "); } root=TreeFromQueue(&head); prettyPrintTree(root, 0); return 0; }