void PreOrder(TreeNode *root) { TreeNode *p = root; stack<TreeNode*> s; while (!s.empty() || p) { while (p) { cout << p->val << endl; s.push(p); p = p->left; } if (!s.empty()) { p = s.top(); s.pop(); p = p->right; } } }
时间: 2024-10-10 04:53:00
void PreOrder(TreeNode *root) { TreeNode *p = root; stack<TreeNode*> s; while (!s.empty() || p) { while (p) { cout << p->val << endl; s.push(p); p = p->left; } if (!s.empty()) { p = s.top(); s.pop(); p = p->right; } } }