我特么要菜哭了
这玩意怎么能卡这么久
Example:
Input: [1,null,2,3]
1
\\
2
/
3
Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>v;
void dfs(TreeNode* root){
if(root->left)
dfs(root->left);
v.push_back(root->val);
if(root->right)
dfs(root->right);
}
vector<int> inorderTraversal(TreeNode* root) {
v.clear();
if(root)
dfs(root);
return v;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>v;
if(root==NULL)
return v;
stack<TreeNode*>s;
s.push(root);
while(s.size()){
TreeNode* tmp=s.top();
if(tmp->left){
s.push(tmp->left);
tmp->left=NULL;
continue;
}
v.push_back(s.top()->val);
s.pop();
if(tmp->right){
s.push(tmp->right);
tmp->right=NULL;
continue;
}
}
return v;
}
};
继续阅读与本文标签相同的文章
-
四方科技:冷链装备龙头 罐箱业务全球领先
2026-05-19栏目: 教程
-
一眼望去 都是中国好CP的形状
2026-05-19栏目: 教程
-
前端开发深水区讨论
2026-05-19栏目: 教程
-
精读《使用 css 变量生成颜色主题》
2026-05-19栏目: 教程
-
震撼!全球首台“智慧旅游黑科技车”现身井陉……
2026-05-19栏目: 教程
