您的位置:首页 > 其它

Leetcode: Binary Tree Inorder Traversal

2013-01-07 11:57 344 查看
//the key is to use a stack smartly
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//if(root==NULL)
//return NULL;
stack<TreeNode*> s;
vector<int> v;
v.clear();
TreeNode* np=root;
while(1){
while(np){
s.push(np);
np=np->left;
}
if(s.empty())
break;
TreeNode* tmp=s.top();
s.pop();
v.push_back(tmp->val);
np=tmp->right;
}
return v;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: