# LintCode 67. 二叉树的中序遍历

67二叉树的中序遍历

### 样例

   1
\
2
/
3

class Solution {
public:
/**
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
vector<int> inorderTraversal(TreeNode * root) {
vector<int> ans;
if(root == NULL)
return ans;
if(root->left != NULL){
vector<int> leftans = inorderTraversal(root->left);
ans.insert(ans.end(),leftans.begin(),leftans.end());
}
ans.push_back(root->val);
if(root->right != NULL){
vector<int> rightans = inorderTraversal(root->right);
ans.insert(ans.end(),rightans.begin(),rightans.end());
}
return ans;
}
};

class Solution {
public:
/**
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
vector<int> inorderTraversal(TreeNode * root) {
vector<int> ans;
if(root == NULL)
return ans;
stack <TreeNode*> st;
st.push(root);
while(st.size()>0){
TreeNode* cur = st.top();
if(cur->left != NULL){
st.push(cur->left);
cur -> left = NULL;
continue;
}
ans.push_back(cur->val);
st.pop();
if(cur->right != NULL){
st.push(cur->right);
cur -> right = NULL;
}
}
return ans;
}
};

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120