Created
January 6, 2018 05:11
-
-
Save sourabh2k15/b74b4f9aaacb50a1668ca61656bfe51e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| void levelOrder(TreeNode* root) { | |
| queue<TreeNode*> bfs; | |
| bfs.push(root); | |
| while(!bfs.empty()){ | |
| TreeNode* current = bfs.front(); bfs.pop(); | |
| if(current){ | |
| cout << current->val << " "; | |
| bfs.push(current->left); | |
| bfs.push(current->right); | |
| } | |
| } | |
| cout << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the above approach, we will end up pushing some unwanted nulls on to the queue. (i am not sure how queue works in CPP)
If before pushing on to the stack if we can check
if(current->left) bfs.push(current.left)we could avoid unwanted pushing of nulls and reduce number of time loops runs