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 ...
随机推荐
- Java中定时器相关实现的介绍与对比之:Timer和TimerTask
Timer和TimerTask JDK自带,具体的定时任务由TimerTask指定,定时任务的执行调度由Timer设定.Timer和TimerTask均在包java.util里实现. 本文基于java ...
- android仿win8
在 eoe上偶然发现已经有人实现了这个功能的源码(地址:http://www.eoeandroid.com /forum.php?mod=viewthread&tid=327557),马上下载 ...
- BluetoothDevice详解
一. BluetoothDevice简介 1. 继承关系 public static Class BluetoothDevice extends Object implement Parcelable ...
- Alpha 冲刺(1/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作,对多个目标检测及文字识别模型进行评估.实验,选取较 ...
- iframe 随内容自适应高度
兼容性好的 html代码: <iframe src="enterprise/enter_edit.aspx" id="mainframe" framebo ...
- (转)linux IO 内核参数调优 之 参数调节和场景分析
1. pdflush刷新脏数据条件 (linux IO 内核参数调优 之 原理和参数介绍)上一章节讲述了IO内核调优介个重要参数参数. 总结可知cached中的脏数据满足如下几个条件中一个或者多个的时 ...
- Python爬虫requests判断请求超时并重新发送请求
下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html 在爬虫的执行当中,总会遇到请求连接 ...
- Angular-Cli中引用第三方库
最近在学习angular(AngularJS 2),根据教程使用angular-cli新建项目, 然而在添加JQuery和Bootstrap第三方库时遇到了问题... 初试 我最初的想法是直接将相对路 ...
- overflow:scroll 滚动条不显示
overflow:scroll 滚动条不显示 ::-webkit-scrollbar-thumb 可能因为 自定义的滚动条height比元素可展示内容大
- BZOJ 口胡记录
最近实在是懒的不想打代码...好像口胡也算一种训练,那就口胡把. BZOJ 2243 染色(树链剖分) 首先树链剖分,然后记录下每个区间的左右端点颜色和当前区间的颜色段.再对每个节点维护一个tag标记 ...