当前位置:网站首页>【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;
}
};
边栏推荐
猜你喜欢

svm.SVC application practice 1--Breast cancer detection

analog IC layout-Environmental noise

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP

mockjs生成假数据的基本使用

Use DBeaver for mysql data backup and recovery

忽晴忽雨

Flask之路由(app.route)详解

Remember a gorm transaction and debug to solve mysql deadlock

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

Oracle19c安装图文教程
随机推荐
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
240...循迹
Outsourcing worked for three years, it was abolished...
29. 删除链表中重复的节点
IPFS deployment and file upload (golang)
【LeetCode】102.二叉树的层序遍历
一次SQL优化,数据库查询速度提升 60 倍
The state status is displayed incorrectly after the openGauss switch
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
FOFAHUB使用测试
【LeetCode】206.反转链表
使用DBeaver进行mysql数据备份与恢复
analog IC layout-Design for reliability
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
JVM调优实战
树链剖分-
isa指针使用详情
What to study after the PMP exam?The soft exam ahead is waiting for you~
VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管