Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

解题思路:

二叉树中序非递归遍历,使用栈来保存遍历到的但是还没有访问其右子树元素的结点;

代码:

 /**
* 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> vals;
stack<TreeNode*> nodes;
TreeNode* node = root; while (node != NULL || !nodes.empty()) {
while (node) {
nodes.push(node);
node = node->left;
} node = nodes.top();
nodes.pop();
vals.push_back(node->val);
node = node->right;
} return vals;
}
};

【Leetcode】【Medium】Binary Tree Inorder Traversal的更多相关文章

  1. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  2. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

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

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

  4. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

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

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

  6. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  7. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  8. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  9. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  10. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

随机推荐

  1. SqlServer索引优化 查看碎片情况

    本文引自 DBCC DBREINDEX重建索引提高SQL Server性能 查看碎片情况使用  dbcc showcontig 函数来进行 代码: --改成当前库 use DB_Name --创建变量 ...

  2. (转)python之os,sys模块详解

    python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...

  3. 使用vmware虚拟机安装linux

  4. Python文件的I/o

    文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 文件I/O 本章只讲述所有基本的的 ...

  5. nginx文件访问403问题

    配置测试环境nginx时,增加了一个新的用户用来上传资源,所以想着就直接把静态资源放在新用户的home目录.为此,还特地设置了相应目录的所有用户都可以读取. chmod 755 /home/tom/s ...

  6. CentOS下MySQL的安装过程

    1 查看 CentOS 自带的 mysql 输入命令: rpm -qa | grep mysql 2 将自带的MySQL卸载了 输入命令: rpm -e --nodeps mysql-libs-5.1 ...

  7. SSL评测

    首先在这个网站上测试一下自己的服务器究竟处于什么水平 https://www.ssllabs.com/ssltest/

  8. ugui的优化

    参考文章 https://www.jianshu.com/p/061e67308e5f https://www.jianshu.com/p/8a9ccf34860e http://blog.jobbo ...

  9. python 对象/变量&赋值的几点思考

    python 对象/变量 对象 Every object has an identity, a type and a value. An object's identity never changes ...

  10. KVC的特殊用法

    - (id)valueForKeyPath:(NSString *)keyPath方法的强大用法,举个例子: NSArray *array = @[@"name", @" ...