当前位置:网站首页>[LeetCode] 94. Inorder traversal of binary tree
[LeetCode] 94. Inorder traversal of binary tree
2022-08-02 02:46:00 【Cake cake ~】
题目
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 .
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1,2,3,4,5,6,7]
输出:[4,2,5,1,6,3,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:
void fun(TreeNode* node,vector<int> &result)
{
if(node->left)
fun(node->left,result);
result.emplace_back(node->val);
if(node->right)
fun(node->right,result);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
if(root)
fun(root,result);
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> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> mystack;
TreeNode* node = root;
while(!mystack.empty() || node!=nullptr)
{
while(node!=nullptr)
{
if(node)
mystack.emplace(node);
node = node->left;
}
node = mystack.top();
mystack.pop();
result.emplace_back(node->val);
node = node->right;
}
return result;
}
};
边栏推荐
- 很有意思的经历,很有意思的项目--文件夹对比工具
- Nacos源码分析专题(一)-环境准备
- Recursively check if a configuration item has changed and replace it
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- OC中成员变量,实例变量和属性之间的区别和联系
- (一)Redis: 基于 Key-Value 的存储系统
- AcWing 1285. Word Problem Solving (AC Automata)
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- 机器人领域期刊会议汇总
- AcWing 1285. 单词 题解(AC自动机)
猜你喜欢
Outsourcing worked for three years, it was abolished...
忽晴忽雨
AI target segmentation capability for fast video cutout without green screen
qt点云配准软件
KICAD 拉线宽度无法修改,解决方法
Chopper webshell feature analysis
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
canal同步Mariadb到Mysql
最大层内元素和
pyqt上手体验
随机推荐
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
【CNN记录】tensorflow slice和strided_slice
通用客户端架构
【web】理解 Cookie 和 Session 机制
analog IC layout-Parasitic effects
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
pyqt上手体验
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
【web】Understanding Cookie and Session Mechanism
mockjs生成假数据的基本使用
很有意思的经历,很有意思的项目--文件夹对比工具
CASE2023
启发式合并、DSU on Tree
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
NIO's Sword
第10章_索引优化与查询优化
Electronic Manufacturing Warehouse Barcode Management System Solution
【每日一道LeetCode】——9. 回文数