We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目描述:
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
解题思路:使用栈进行遍历
C++解题:
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { if (!root) return {}; vector <int> result; stack<TreeNode *> ss; ss.push(root); while (!ss.empty()) { TreeNode *node = ss.top(); ss.pop(); result.push_back(node->val); if(node->right) ss.push(node->right); if(node->left) ss.push(node->left); } return result; } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述:
给定一个二叉树,返回它的 前序 遍历。
示例:
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
解题思路:使用栈进行遍历
C++解题:
The text was updated successfully, but these errors were encountered: