Leetcode 94. 二叉树的中序遍历
1.问题描述
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
2.解法一:递归
中序遍历:L--N--R (左--根--右)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left); //左
res.push_back(root->val); //根
inorderTraversal(root->right);//右
}
return res;
}
private:
vector<int> res;
};
3.递归与迭代的区别
- 递归:A反复调用A自身
- 迭代:A不停调用B (B是利用变量的原值推算出变量的一个新值)
(1)递归中一定有迭代,但是迭代中不一定有递归;
(2)能使用迭代尽量使用迭代
4.解法二:非递归(栈)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
//s.push(root);
TreeNode* cur = root;// //L--N--R
while(cur || !s.empty())
if(cur){
s.push(cur);
cur = cur->left; //先走到最左子树
}
else{
res.push_back(s.top()->val);
cur = s.top()->right;
s.pop();
}
return res;
}
private:
vector<int> res;
};
5.解法三:线索二叉树(不用递归不用栈)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
//判空
if (!root) return res;
TreeNode *cur, *pre;//当前指针cur,前驱指针pre
cur = root;
//二叉树线索化&中序遍历
while (cur){
//左子树为空时
if (!cur->left){
res.push_back(cur->val);
cur = cur->right;
}
//左子树不空时
else{
pre = cur->left;
while (pre->right && pre->right != cur)
{
pre = pre->right;
}
//pre->right为空
if (!pre->right){
pre->right = cur;
cur = cur->left;
}
//pre->right不空
else{
pre->right = NULL;
res.push_back(cur->val);
cur = cur->right;
}
}
}
return res;
}
//数据的封装
private:
vector<int> res;
};
参考资料:
1.https://blog.csdn.net/swliao/article/details/5337896 递归与迭代的区别
2.https://www.cnblogs.com/ariel-dreamland/p/9159638.html
Leetcode 94. 二叉树的中序遍历的更多相关文章
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode 94. 二叉树的中序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...
- LeetCode 94 ——二叉树的中序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
随机推荐
- 孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解
孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解 (今天由于文中所阐述的原因没有进行屏幕录屏,见谅) 为了能够使用selenium模块进行真正的操作,今天主要大范围搜索资料进行 ...
- 记一次Log4j2日志无法输出的 心酸史
问题描述:部分日志无法输出到日志文件中. 项目中的代码: @Resource private ConfigInfo configInfo; private static final Logger lo ...
- [HNOI2018]寻宝游戏(题解转载自别处)
题解(自别处转载): Luogu CSDN 这题关键是将运算符也替换成0,1 然后在运算符与原串混杂里找规律. 而且替换的方式也有所要求,考场上两种替换方式都要尝试. #include <bit ...
- 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)
任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...
- [C++ map & dp]codeforces 960F. Pathwalks
题目传送门:960F 思路: 题目给人的感觉很像最长上升子序列,自然而然想到用dp的思路去处理 题目中给的限制条件是,要接上前面的边,前面的边权一定要小于当前的边权(题目按照输入的顺序,因此只找前面的 ...
- 简单构建基于RDF和SPARQL的KBQA(知识图谱问答系统)
本文主要通过python实例讲解基于RDF和SPARQL的KBQA系统的构建.该项目可在python2和python3上运行通过. 注:KBQA即是我们通常所说的基于知识图谱的问答系统.这里简单构建的 ...
- 十:HDFS Short-Circuit Local Reads 短路本地读取
当client请求数据时,datanode会读取数据然后通过TCP协议发送给client.short-circuit绕过了datanode直接读取数据.short-circuit的前提是client和 ...
- 《剑指Offer》题二十一~题三十
二十一.调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 测试用例: 功能测试:输入数组中的奇 ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- Thunder团队第二周 - Scrum会议5
Scrum会议5 小组名称:Thunder 项目名称:爱阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...