题目
给出一棵二叉树,返回其节点值的前序遍历。
和中序遍历基本相同
C++代码
vector preorderTraversal(TreeNode *root) { // write your code here vector vec; stacks; TreeNode* p = root; while (p || !s.empty()) { while (p) { vec.push_back(p->val); s.push(p); p = p->left; } p = s.top(); s.pop(); p = p->right; } return vec;}