当前位置:网站首页>【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;
}
};
边栏推荐
- 2022-08-01 Reflection
- 2022牛客多校三_F G
- KICAD 小封装拉线卡顿问题 解决方法
- Lombok
- 项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
- Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- 简单的页面跳转活动
- 考完PMP学什么?前方软考等着你~
- 60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
猜你喜欢
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
2022牛客多校四_G M
【web】Understanding Cookie and Session Mechanism
pyqt上手体验
240...循迹
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
The failure to create a role in Dahua Westward Journey has been solved
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
KICAD 小封装拉线卡顿问题 解决方法
Oracle19c安装图文教程
随机推荐
OC和Swift语言的区别
KICAD 小封装拉线卡顿问题 解决方法
永磁同步电机36问(二)——机械量与电物理量如何转化?
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
Analysis of the status quo of digital transformation of manufacturing enterprises
GTK RGB图像绘制
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
记一个gorm初始化的坑
Outsourcing worked for three years, it was abolished...
A good book for newcomers to the workplace
OperatingSystemMXBean获取系统性能指标
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
JVM调优实战
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
FOFAHUB usage test
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
2022 Henan Youth Training League Game (3)
1688以图搜货
NIO's Sword