Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Created December 26, 2017 01:33
Show Gist options
  • Save sourabh2k15/c1e19a0f8d7fb2a9a788dd64269d37ef to your computer and use it in GitHub Desktop.
Save sourabh2k15/c1e19a0f8d7fb2a9a788dd64269d37ef to your computer and use it in GitHub Desktop.
Leetcode #94 Inorder Traversal of Binary Tree
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