Binary Tree Inorder Traversal

来源:互联网 发布:怎么查淘宝买家信息 编辑:程序博客网 时间:2024/06/15 08:28

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;    }
0 0