当前位置:网站首页>【LeetCode】144. Preorder Traversal of Binary Tree
【LeetCode】144. Preorder Traversal of Binary Tree
2022-08-02 02:46:00 【Crispy~】
题目
给你二叉树的根节点 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年NPDP考完多久出成绩?怎么查询?
- Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- 淘宝详情.
- 罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- The failure to create a role in Dahua Westward Journey has been solved
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- Chrome浏览器无法加载已解压的.crx文件的解决办法
- svm.SVC application practice 1--Breast cancer detection
猜你喜欢
随机推荐
启发式合并、DSU on Tree
MySQL - CRUD operations
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
记一次gorm事务及调试解决mysql死锁
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
【web】理解 Cookie 和 Session 机制
JS中获取对象数据类型的键值对的键与值
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
Install mysql using docker
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
KICAD 小封装拉线卡顿问题 解决方法
OC中成员变量,实例变量和属性之间的区别和联系
How ReentrantLock works
线程的不同状态
Safety (1)
一次SQL优化,数据库查询速度提升 60 倍
字典常用方法
The failure to create a role in Dahua Westward Journey has been solved
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
Use DBeaver for mysql data backup and recovery