您的位置:首页 > 其它

Binary Tree Inorder Traversal

2015-09-08 15:05 274 查看
Given a binary tree, return the inorder traversal of its nodes’ values.

For example: Given binary tree {1,#,2,3},

1

\

2

/

3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

题意:中序遍历

vector<int> InOrderTraversal(TreeNode *root) {
vector<int> path;
stack<TreeNode*> stk;
while(root != NULL || !stk.empty())
{
while(root != NULL)
{
stk.push(root);
root = root->left;
}
if( !stk.empty())
{
root = stk.top();
stk.pop();
root = root->right;
}
}

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