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

For example:
Given binary tree [1,null,2,3],

   1
\
2
/
3

return [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) {
vector<int> results;
if(root == NULL)
return results; stack<TreeNode*> nodes;
nodes.push(root);
TreeNode* curr;
int shouldFindLeft = ;
while(!nodes.empty())
{
curr = nodes.top(); // add all left nodes
if(shouldFindLeft == )
{
while(curr->left != NULL)
{
nodes.push(curr->left);
curr = curr -> left;
}
} // print curr node
results.push_back(curr->val);
nodes.pop(); // add its right child
if(curr->right != NULL)
{
nodes.push(curr->right);
shouldFindLeft = ;
}
else
{
shouldFindLeft = ;
} } return results;
}
};

leetcode笔记(二)94. Binary Tree Inorder Traversal的更多相关文章

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

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

  2. 49. leetcode 94. Binary Tree Inorder Traversal

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

  3. 刷题94. Binary Tree Inorder Traversal

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

  4. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

  6. 【一天一道LeetCode】#94. Binary Tree Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

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

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

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

  9. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Neutron命令测试3

    1.打开Ubuntu的/etc/network/interfaces文件 .默认内容如下 auto lo iface lo inet loopback 2.如果以DHCP方式配置网卡,则改为:auto ...

  2. c#-day04学习笔记

    面向对象 类与对象: C#的类和对象是用于在程序中模拟现实生活中的事务的 C#中的类是一种数据类型,用来定义对象的类型的 C#的对象是类的实例,是基于[给定数据类型]的具体的一个实例 小结: 类是对象 ...

  3. php 设置虚拟目录

    php 设置虚拟目录两个步骤,很简单. 第一,打开安装apache目录下的conf下面的httpd.conf文件.   找到IfModule 标记, 再它下面添加红色方框中的内容.注意不能少东西.   ...

  4. Win7无线路由Win8/8.1开启网络承载DOS命令笔记本电脑当无线路由器

    以下废话,先吐槽几句 这个功能其实在Win7+系统都内置了,不要再用什么某豹.某大师.某Soft之类的开启无线网络,这些软件都是骗你们安装一些垃圾软件的. 另外吐槽 某度wifi.某数字wifi都是垃 ...

  5. 用户“Michael-PC\Michael”不具有所需的权限。请验证授予了足够的权限并且解决了 Windows 用户帐户控制(UAC)限制问题。

    解决方法:在注册表增加键值 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] 新建-选择“D ...

  6. Maven,SVN,快捷键,数据库等

    1.Eclipse中Maven的搭建: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven 下载地址: http://maven. ...

  7. maven课程 项目管理利器-maven 1-1课程概述

    1 为什么使用maven? 多框架应用项目,jar包太多且冲突,为了解决这个问题,引入maven.(类似还有ant,gradle) 2 课程概述 maven快速入门 maven核心知识 maven建造 ...

  8. git 提交各种情况下的处理方式

    自己总结: 01.若在提交过程中有冲突,解决冲突后,git add . git rebase —continue git push for   02.git rebase vs git merge g ...

  9. Android Studio 导入 AOSP 源码

    有了 AOSP 源码,接下来就是如何看了,可以直接文本看,可以用 Source Insight,我当然选择 Android Studio,Android Studio 是我熟悉且十分强大的工具.问题来 ...

  10. Java—运算符和流程控制语句

    运算符优先级: siwtch语句注意事项: switch 后面小括号中表达式的值必须是整型或字符型 case 后面的值可以是常量数值,如 1.2:也可以是一个常量表达式,如 2+2 :但不能是变量或带 ...