Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- queue<TreeNode*> q; // To store the node addresses
- vector<int> result; // To store the values in the node
- result.push_back(root->val);
- q.push(root);
- while (!q.empty()) {
- node = q.front();
- q.pop();
- if(node->left){
- result.push_back(node->left->val);
- q.push(node->left);
- }else{
- result.push_back(NULL);
- }
- if(node->right){
- result.push_back(node->right->val);
- q.push(node->right);
- }else{
- result.push_back(NULL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement