题目意思:二叉树中序遍历,结果存在vector<int>中

解题思路:迭代

迭代实现:

 /**
* 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) {
vector<int> ans;
if(root){
stack<TreeNode*> s;
TreeNode* temp=root;
while(temp!=NULL||!s.empty()){
while(temp!=NULL){
s.push(temp); //将所有的左节点入栈
temp=temp->left;
}
temp=s.top();
s.pop();
ans.push_back(temp->val); //添加最下面的左节点
temp=temp->right; //添加倒数第二个左节点的右节点
}
}
return ans;
}
};

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

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

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

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

  4. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

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

  5. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

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

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

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

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

  8. Leetcode 94 Binary Tree Inorder Traversal 二叉树

    二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...

  9. 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历

    题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...

随机推荐

  1. 数据结构(堆):SCOI 2009 生日礼物

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩 珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个 ...

  2. 【数论】FOJ 2238 Daxia & Wzc's problem

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...

  3. 使用DateDiff方法获取日期时间的间隔数

    一:用DateDiff方法获取日期时间的间隔数,截图 二:代码 using System; using System.Windows.Forms; using Microsoft.VisualBasi ...

  4. Redis源码阅读笔记(2)——字典(Map)实现原理

    因为redis是用c写的,c中没有自带的map,所以redis自己实现了map,来看一下redis是怎么实现的. 1.redis字典基本数据类型 redis是用哈希表作为字典的底层实现,dictht是 ...

  5. c#反序列化

    C#序列化(转载) 2011-03-15  |  凯之风  |  转藏(2) 序列化有下列要求: A> 只序列化PUBLIC的成员变量和属性 B> 类必须有默认识构造函数 C> 如果 ...

  6. C语言学习_从VC++6.0开始

    前言: C语言是一门博大精深的语言,C语言往往是程序员以及所有软件行业从业者的第一门编程语言. 编程环境: 对于初学者来说,我一开始学习C语言,其实是用的turboc 2.0版本,这个很有历史感,但是 ...

  7. [Javascript] Logging Pretty-Printing Tabular Data to the Console

    Learn how to use console.table to render arrays and objects in a tabular format for easy scanning ov ...

  8. [Angualr 2] Watch for changes

    You can watch for form / control changes by using .valueChanges.observe({...}): this.sku.valueChange ...

  9. memcache实现公共计数器网站

    在反问题的过程中遇到的最近项目.网上查了很多资料并没有完全实现. 因此,要找到适合自己的xmemcache client和memcache关联API和说明,我们发现了一个比较完美的实现. 键类:net ...

  10. S2SH框架集成详解(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)

    近期集成了一次较新版本的s2sh,出现了不少问题,网上资料也是良莠不齐,有的甚至就是扯淡,简单的把jar包扔进去就以为是集成成功了,在这里整理一下详细的步骤,若哪位有什么不同看法,可以留言,欢迎批评改 ...