当前位置:网站首页>【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;
}
};
边栏推荐
- analog IC layout-Parasitic effects
- VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管
- What to study after the PMP exam?The soft exam ahead is waiting for you~
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- 1688以图搜货
- * 比较版本号
- 网络层解析——IP协议、地址管理、路由选择
- Install mysql using docker
- IPFS部署及文件上传(golang)
猜你喜欢
随机推荐
Safety (1)
FOFAHUB使用测试
使用docker安装mysql
The state status is displayed incorrectly after the openGauss switch
analog IC layout-Environmental noise
很有意思的经历,很有意思的项目--文件夹对比工具
How ReentrantLock works
简单的页面跳转活动
永磁同步电机36问(三)——SVPWM代码实现
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
最大层内元素和
analog IC layout
Remember a gorm transaction and debug to solve mysql deadlock
周鸿祎称微软抄袭,窃取360安全模式
How engineers treat open source
Flask入门学习教程
analog IC layout-Parasitic effects
【LeetCode】102.二叉树的层序遍历
【web】Understanding Cookie and Session Mechanism
使用self和_(下划线)的区别









