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

FOFAHUB使用测试

罗德里格斯公式(Rodrigues‘ Rotation Formula)推导

analog IC layout-Design for reliability

国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现

Install mysql using docker

工程师如何对待开源

ReentrantLock工作原理

Nacos源码分析专题(二)-服务注册

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects

接口测试神器Apifox究竟有多香?
随机推荐
周鸿祎称微软抄袭,窃取360安全模式
Lombok
789. 数的范围
灰度传感器、、、diy原理。。图
【LeetCode】83.删除排序链表中的重复元素
Install mysql using docker
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
有人知道HTML怎么到MYSQL数据库吗? (NODEJS)
欧拉公式的证明
递归检查配置项是否更变并替换
aws s3 upload file
考完PMP学什么?前方软考等着你~
【web】理解 Cookie 和 Session 机制
1688API
启发式合并、DSU on Tree
2022.8.1-----leetcode.1374
VPS8701 电源管理(PMIC) VPS8701
ApiFox 基本使用教程(浅尝辄止,非广)
【LeetCode】145.二叉树的后序遍历
Remember a gorm transaction and debug to solve mysql deadlock