Binary Tree Inorder Traversal @LeetCode
PROBLEM :
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution )
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
InorderRecursive(root,vec) ;
return vec ;
}
void InorderRecursive(TreeNode* root,vector<int> &vec){
if(root==NULL)
return ;
InorderRecursive(root->left,vec) ;
vec.push_back(root->val) ;
InorderRecursive(root->right,vec) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Iterative Solution )
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta ;
while(true){
if(root){
sta.push(root) ;
root=root->left ;
}
else
{
if(sta.empty())
break ;
TreeNode* node ;
node=sta.top() ;
sta.pop() ;
vec.push_back(node->val) ;
root=node->right ;
}
}
return vec ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution - Morris Traversal)
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
TreeNode* curr,*prev ;
curr=root ;
while(curr!=NULL){
if(curr->left==NULL){
vec.push_back(curr->val) ;
curr=curr->right ;
}
else{
prev=curr->left ;
while(prev->right&&prev->right!=curr)
prev=prev->right ;
if(prev->right==NULL){
prev->right=curr ;
curr=curr->left ;
}
else{
prev->right=NULL ;
vec.push_back(curr->val) ;
curr=curr->right ;
}
}
}
return vec ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution )
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
InorderRecursive(root,vec) ;
return vec ;
}
void InorderRecursive(TreeNode* root,vector<int> &vec){
if(root==NULL)
return ;
InorderRecursive(root->left,vec) ;
vec.push_back(root->val) ;
InorderRecursive(root->right,vec) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Iterative Solution )
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta ;
while(true){
if(root){
sta.push(root) ;
root=root->left ;
}
else
{
if(sta.empty())
break ;
TreeNode* node ;
node=sta.top() ;
sta.pop() ;
vec.push_back(node->val) ;
root=node->right ;
}
}
return vec ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive Solution - Morris Traversal)
--------------------------------------------------------------------------------
/**
* 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> vec ;
if(root==NULL)
return vec ;
TreeNode* curr,*prev ;
curr=root ;
while(curr!=NULL){
if(curr->left==NULL){
vec.push_back(curr->val) ;
curr=curr->right ;
}
else{
prev=curr->left ;
while(prev->right&&prev->right!=curr)
prev=prev->right ;
if(prev->right==NULL){
prev->right=curr ;
curr=curr->left ;
}
else{
prev->right=NULL ;
vec.push_back(curr->val) ;
curr=curr->right ;
}
}
}
return vec ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment