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 ...
随机推荐
- (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(3)修正
@author: 白袍小道 转载说明原处,爱护劳动 插件同步在GITHUB: DaoZhang_XDZ 说明 1.本篇是接着-----(原) MaterialEditor部- Umat ...
- CsvHelper文档-1前言
CsvHelper文档-1前言 英文文档链接地址:CsvHelper Document 开源项目地址:CsvHelper 翻译于2018-1-5,原本可能会随时更新: 每一段代码都是经过我实际测试的, ...
- numpy切片和布尔型索引
numpy 标签(空格分隔): numpy 数据挖掘 切片 数组切片是原始数组的视图.这意味着数据不会被复制,视图上的任何修改都会直接反映到源数组上 In [16]: arr Out[16]: arr ...
- scatter注记词2
couch ranch bind ski extra bring note embrace tape they stick legend
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Linux 应用笔记
Linux 应用笔记 Linux 应用笔记 小书匠 Raspberry Pi 常用命令 CentOs Raspberry Ubuntu python 实用教程 Vim 权限问题 内存分配 shell ...
- 第八次作业——项目UML设计
分工及贡献分评定 成员 参与 贡献比例 朱跃安(031602348) 类图 13% 后敬甲(031602409) 实体关系图+博客整理 14.5% 林志华(031602128) 用例图+活动图 14. ...
- phpshell提权
实际操作中可以在webshell用udf.dll提权,用函数的上传文件功能上传文件到启动目录,再用shut函数重起系统.(目前没成功过,有 机会本地测试一下,先记录在这了).如果是英文版的系统,启动目 ...
- VUE01指令
一.下载Vue2.0的两个版本: 官方网站:http://vuejs.org/ 开发版本:包含完整的警告和调试模式 生产版本:删除了警告,进行了压缩 二.项目结构搭建 这个部分要视频中有详细讲解. 三 ...
- mysql三种备份方式
一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...