1.问题描述

给定一个二叉树,返回它的中序 遍历。

示例:

  1. 输入: [1,null,2,3]
  2. 1
  3. \
  4. 2
  5. /
  6. 3
  7.  
  8. 输出: [1,3,2]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

2.解法一:递归

中序遍历:L--N--R (左--根--右)

  1. class Solution {
  2. public:
  3. vector<int> inorderTraversal(TreeNode* root) {
  4. if(root){
  5. inorderTraversal(root->left); //左
  6. res.push_back(root->val); //根
  7. inorderTraversal(root->right);//右
  8. }
  9. return res;
  10. }
  11. private:
  12. vector<int> res;
  13. };

3.递归与迭代的区别

  • 递归:A反复调用A自身
  • 迭代:A不停调用B (B是利用变量的原值推算出变量的一个新值)
注意:
(1)递归中一定有迭代,但是迭代中不一定有递归;
(2)能使用迭代尽量使用迭代

4.解法二:非递归(栈)

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> inorderTraversal(TreeNode* root) {
  13. stack<TreeNode*> s;
  14. //s.push(root);
  15. TreeNode* cur = root;//
  16.  
  17. //L--N--R
  18. while(cur || !s.empty())
  19. if(cur){
  20. s.push(cur);
  21. cur = cur->left; //先走到最左子树
  22. }
  23. else{
  24. res.push_back(s.top()->val);
  25. cur = s.top()->right;
  26. s.pop();
  27. }
  28. return res;
  29. }
  30. private:
  31. vector<int> res;
  32. };

5.解法三:线索二叉树(不用递归不用栈)

  1. class Solution {
  2. public:
  3. vector<int> inorderTraversal(TreeNode* root) {
  4. //判空
  5. if (!root) return res;
  6.  
  7. TreeNode *cur, *pre;//当前指针cur,前驱指针pre
  8. cur = root;
  9. //二叉树线索化&中序遍历
  10. while (cur){
  11. //左子树为空时
  12. if (!cur->left){
  13. res.push_back(cur->val);
  14. cur = cur->right;
  15. }
  16. //左子树不空时
  17. else{
  18. pre = cur->left;
  19. while (pre->right && pre->right != cur)
  20. {
  21. pre = pre->right;
  22. }
  23. //pre->right为空
  24. if (!pre->right){
  25. pre->right = cur;
  26. cur = cur->left;
  27. }
  28. //pre->right不空
  29. else{
  30. pre->right = NULL;
  31. res.push_back(cur->val);
  32. cur = cur->right;
  33. }
  34. }
  35. }
  36. return res;
  37. }
  38.  
  39. //数据的封装
  40. private:
  41. vector<int> res;
  42. };

参考资料:

1.https://blog.csdn.net/swliao/article/details/5337896 递归与迭代的区别

2.https://www.cnblogs.com/ariel-dreamland/p/9159638.html

Leetcode 94. 二叉树的中序遍历的更多相关文章

  1. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  2. Java实现 LeetCode 94 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...

  3. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...

  4. leetcode 94二叉树的中序遍历

    递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  5. 【leetcode 94. 二叉树的中序遍历】解题报告

    前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...

  6. LeetCode 94 ——二叉树的中序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...

  7. 【LeetCode】94. 二叉树的中序遍历

    94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...

  8. Leetcode题目94.二叉树的中序遍历(中等)

    题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...

  9. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

随机推荐

  1. (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(3)修正

    @author: 白袍小道 转载说明原处,爱护劳动 插件同步在GITHUB: DaoZhang_XDZ         说明 1.本篇是接着-----(原) MaterialEditor部- Umat ...

  2. CsvHelper文档-1前言

    CsvHelper文档-1前言 英文文档链接地址:CsvHelper Document 开源项目地址:CsvHelper 翻译于2018-1-5,原本可能会随时更新: 每一段代码都是经过我实际测试的, ...

  3. numpy切片和布尔型索引

    numpy 标签(空格分隔): numpy 数据挖掘 切片 数组切片是原始数组的视图.这意味着数据不会被复制,视图上的任何修改都会直接反映到源数组上 In [16]: arr Out[16]: arr ...

  4. scatter注记词2

    couch ranch bind ski extra bring note embrace tape they stick legend

  5. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. Linux 应用笔记

    Linux 应用笔记 Linux 应用笔记 小书匠 Raspberry Pi 常用命令 CentOs Raspberry Ubuntu python 实用教程 Vim 权限问题 内存分配 shell ...

  7. 第八次作业——项目UML设计

    分工及贡献分评定 成员 参与 贡献比例 朱跃安(031602348) 类图 13% 后敬甲(031602409) 实体关系图+博客整理 14.5% 林志华(031602128) 用例图+活动图 14. ...

  8. phpshell提权

    实际操作中可以在webshell用udf.dll提权,用函数的上传文件功能上传文件到启动目录,再用shut函数重起系统.(目前没成功过,有 机会本地测试一下,先记录在这了).如果是英文版的系统,启动目 ...

  9. VUE01指令

    一.下载Vue2.0的两个版本: 官方网站:http://vuejs.org/ 开发版本:包含完整的警告和调试模式 生产版本:删除了警告,进行了压缩 二.项目结构搭建 这个部分要视频中有详细讲解. 三 ...

  10. mysql三种备份方式

    一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...