这道题是LeetCode里的第94道题。

题目要求:

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

示例:

输入: [1,null,2,3]
1
\
2
/
3 输出: [1,3,2]

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

解题代码:

/**
* 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*>st;
TreeNode* pt;
vector<int>res;
if(root==NULL)
return res;
pt=root;
while(pt!=NULL||st.size()!=0){
while(pt!=NULL){
st.push(pt);
pt=pt->left;
}
pt=st.top();
st.pop();
res.push_back(pt->val);
pt=pt->right;
}
return res;
}
};

提交结果:

个人总结:

先左后右,推荐和前序遍历后序遍历一起看,比较一下代码的不同。

【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)的更多相关文章

  1. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  2. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  4. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

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

  5. [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆

    二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...

  6. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. [Leetcode] Binary tree inorder traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. cocos2d-x入门学习篇;切换场景

    手机游戏开发最近很火爆,鉴于一直在学习c++,看起来上手就比较快了.这篇文章来自皂荚花 cocos2d-x技术,我把我的想法分享给大家. 首先来看一段代码: CCScene* HelloWorld:: ...

  2. sqlserver创建触发器

    Create TRIGGER [dbo].[tr_Delete_AllocationedDN] --删除指定账户已分配未拣货的任务 ON [dbo].[FRU_PickLocationNew] --触 ...

  3. Delphi7使用二维码

    参考:http://jingyan.baidu.com/article/e75057f2ad6481ebc81a897b.html 首先下载对应的 dll (已经上传到博客园文件) 然后就是Delph ...

  4. java面试题(杨晓峰)---第四讲强引用、软引用、弱引用、幻想引用有什么区别?

    在java语言中,除了原始数据类型的变量,其他所有都是所谓的引用类型,指向各种不同的对象,理解引用对于掌握java对象生命周期和JVM内部相关机制非常有帮助. 今天问题:强引用.软引用.弱引用.幻想引 ...

  5. HDU 4284 Travel (Folyd预处理+dfs暴搜)

    题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不 ...

  6. [神经网络]一步一步使用Mobile-Net完成视觉识别(四)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第四篇,下载预训练模型并训练自己的数据集. 前面我们配置好了labelmap,下面我们开始下载训练好的 ...

  7. (六)VMware Harbor简单使用

    VMware Harbor简单使用 1. 登陆: [用户:admin  , 密码:Harbor12345]配置文件里设置的 登陆后的界面: 2. 用户管理: 2.1 新近用户 3. 仓库管理: 3.1 ...

  8. Ubuntu系统Apache 2部署SSL证书

    几天前用Apache 2部署了一个静态网页,但通过域名访问时Google提示“不安全”,经了解,原来是缺少证书. 什么是SSL证书? SSL 是指安全套接字层,简而言之,它是一项标准技术,可确保互联网 ...

  9. HTML之基本语法(链接标签、路径的介绍和使用)

    一.链接标签 语法:<a href="目标地址">这个标签上展示的内容</a> 作用:可以实现在当前页面跳转到新页面的操作 属性 1.target这个属性可 ...

  10. SC || Git 相关知识

    发现大佬的博客:https://www.cnblogs.com/onetwo/p/4157610.html 注:图片来自小姐姐的笔记~ ┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉┉∞ ∞┉┉┉┉∞ ...