Created
December 26, 2017 01:33
-
-
Save sourabh2k15/c1e19a0f8d7fb2a9a788dd64269d37ef to your computer and use it in GitHub Desktop.
Leetcode #94 Inorder Traversal of Binary Tree
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
class Solution { | |
public: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> inorder; | |
stack<TreeNode*> nodes; | |
if(root == NULL) return inorder; | |
while(root != NULL || !nodes.empty()){ | |
//push left children if available | |
while(root != NULL){ | |
nodes.push(root); | |
root = root->left; | |
} | |
//retrieve top node and store its right child if exists | |
root = nodes.top(); | |
nodes.pop(); | |
inorder.push_back(root->val); | |
root = root->right; | |
} | |
return inorder; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment