当前位置:网站首页>【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;
}
};
边栏推荐
猜你喜欢
CASE2023
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
ofstream,ifstream,fstream read and write files
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
aws s3上传文件
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
Docker-compose安装mysql
详解最强分布式锁工具:Redisson
菜刀webshell特征分析
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
随机推荐
2022-08-01 Install mysql monitoring tool phhMyAdmin
MySQL索引优化实战
analog IC layout-Design for reliability
mysql 查看死锁
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
analog IC layout-Environmental noise
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
JS中获取对象数据类型的键值对的键与值
Can Youxuan database import wrongly be restored?
菜刀webshell特征分析
架构:分布式任务调度系统(SIA-Task)简介
svm.SVC应用实践1--乳腺癌检测
Nacos源码分析专题(一)-环境准备
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
Swift运行时(派发机制)
字典常用方法
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
How to adjust the cross cursor too small, CAD dream drawing calculation skills
接口测试神器Apifox究竟有多香?
【web】理解 Cookie 和 Session 机制