当前位置:网站首页>【LeetCode】144.二叉树的前序遍历
【LeetCode】144.二叉树的前序遍历
2022-08-02 02:40:00 【酥酥~】
题目
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
示例 1:
输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1,2,3,4,5,6,7]
输出:[1,2,4,5,3,6,7]
提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
进阶:递归算法很简单,你可以通过迭代算法完成吗?
题解
深度优先遍历
使用迭代法
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
if(root==nullptr)
return {
};
vector<int> result;
stack<TreeNode*> mystack;
mystack.push(root);
while(!mystack.empty())
{
TreeNode* tmp = mystack.top();
mystack.pop();
result.emplace_back(tmp->val);
if(tmp->right)
mystack.emplace(tmp->right);
if(tmp->left)
mystack.emplace(tmp->left);
}
return result;
}
};
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
if(root==nullptr)
return {
};
vector<int> result;
stack<TreeNode*> mystack;
TreeNode* node = root;
while(!mystack.empty() || node!=nullptr)
{
while(node!=nullptr)
{
result.emplace_back(node->val);
mystack.emplace(node);
node = node->left;
}
node = mystack.top();
mystack.pop();
node = node->right;
}
return result;
}
};
使用递归的方法
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
void fun(TreeNode* node,vector<int> &result)
{
result.emplace_back(node->val);
if(node->left)
fun(node->left,result);
if(node->right)
fun(node->right,result);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
if(root)
fun(root,result);
return result;
}
};
边栏推荐
猜你喜欢

永磁同步电机36问(三)——SVPWM代码实现

The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string

四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料

Install mysql using docker

【web】理解 Cookie 和 Session 机制

忽晴忽雨

【web】Understanding Cookie and Session Mechanism

BI-SQL丨WHILE

Remember a gorm transaction and debug to solve mysql deadlock

【每日一道LeetCode】——9. 回文数
随机推荐
mockjs生成假数据的基本使用
Power button 1374. Generate each character string is an odd number
微服务:微智能在软件系统的简述
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
BI - SQL 丨 WHILE
简单的页面跳转活动
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
AI目标分割能力,无需绿幕即可实现快速视频抠图
最大层内元素和
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
架构:应用架构的演进以及微服务架构的落地实践
789. 数的范围
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
How engineers treat open source
永磁同步电机36问(二)——机械量与电物理量如何转化?
使用DBeaver进行mysql数据备份与恢复
Flask入门学习教程
Unable to log in to the Westward Journey
OC中new和init的区别
Qt自定义控件和模板分享