Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
Given binary tree
{1,#,2,3}
,1 \ 2 / 3
return
[1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
Solution 1 (Recursive)
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
void helper(TreeNode* node, vector<int>& savedNodes){ | |
if(node->left!=NULL) | |
helper(node->left, savedNodes); | |
savedNodes.push_back(node->val); | |
if(node->right!=NULL) | |
helper(node->right, savedNodes); | |
} | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> nodes; | |
if(root==NULL) | |
return nodes; | |
helper(root, nodes); | |
return nodes; | |
} | |
}; |
Solution 2 (Non-recursive)
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> nodes; | |
if(root==NULL) | |
return nodes; | |
stack<TreeNode*> myStack; | |
myStack.push(root); | |
unordered_map<TreeNode*, int> myMap; | |
while(!myStack.empty()){ | |
TreeNode* topNode=myStack.top(); | |
if(topNode->left!=NULL && myMap[topNode->left]!=1){ | |
myStack.push(topNode->left); | |
continue; | |
} | |
nodes.push_back(topNode->val); | |
myMap[topNode]=1; | |
myStack.pop(); | |
if(topNode->right!=NULL && myMap[topNode->right]!=1){ | |
myStack.push(topNode->right); | |
continue; | |
} | |
} | |
return nodes; | |
} | |
}; |
Round 2 solution:
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> res; | |
if(root==NULL) | |
return res; | |
stack<TreeNode*> myStack; | |
TreeNode* p=root->left; | |
myStack.push(root); | |
while(true){ | |
if(!p){ | |
if(myStack.empty()) | |
break; | |
TreeNode* top = myStack.top(); | |
myStack.pop(); | |
res.push_back(top->val); | |
p = top->right; | |
}else{ | |
myStack.push(p); | |
p = p->left; | |
} | |
} | |
return res; | |
} | |
}; |
No comments:
Post a Comment