题目:Given a binary tree, return the inorder traversal of its nodes' values.
Given binary tree {1,#,2,3}
,
return [1,3,2]
.
思路:左节点-->根节点-->右节点
如果节点不为空或者堆栈不为空,入栈。不行了,就出栈。
怕就怕,root已经为空,但是堆栈不为空,所以在while循环设立判断条件。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//https://leetcode.com/problems/binary-tree-inorder-traversal/
class Solution1 {
public:
vector<int> inorderTraversal1(TreeNode* root){
vector<int> result;
inorderHelper(root,result);
return result;
}
void inorderHelper(TreeNode *root,vector<int> &result){
if(root==NULL) return;
inorderHelper(root->left,result);
result.push_back(root->val);
inorderHelper(root->right,result);
}
};
class Solution2 {
public:
vector<int> inorderTraversal2(TreeNode* root){
stack<TreeNode *>st;
vector<int> result;
while(!(root==NULL&&st.empty())){
if(root!=NULL){
st.push(root);
root=root->left;
}
else{
root=st.top();st.pop();
result.push_back(root->val);
root=root->right;
}
//这个时候while循环是不对的,如果只有左子树,就不能出结果。因为有堆栈,可以判断是否为空。
}
return result;
}
};