class Solution{ public: vector<int>preorderTraversal(TreeNode *root) { vector<int>preOrder; Traversal(root,preOrder); return preOrder; } stack<TreeNode*> st; private: void Traversal(TreeNode *root,vector<int>&preOrder) { if(root==NULL) return; TreeNode *p; TreeNode *pre=NULL; bool flag=true; p=root; while(p||!st.empty()) { if(p) { st.push(p); preOrder.push_back(p->val); p=p->left; } else { p=st.top(); st.pop(); p=p->right; } } } };
Binary Tree Preorder Traversal,布布扣,bubuko.com
Binary Tree Preorder Traversal
原文地址:http://www.cnblogs.com/dupuleng/p/3818183.html