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. Host 'XXX' is not allowed to connect to this MySQL server解决方案

    如何允许远程连接mysql数据库呢,操作如下: 首先登录账号 mysql -uroot -p 使用mysql用户 use mysql 如果报此类错:ERROR 1820 (HY000): You mu ...

  2. php 自带加密、解密函数

    php 自带的加密函数  不可逆的加密函数为:md5().crypt() md5() 用来计算 MD5 哈稀.语法为:string md5(string str); crypt() 将字符串用 UNI ...

  3. Git提交代码到主分区

    git 提交代码,本地新建一个my分支,不从本地master分支直接上传,而是先从本地my分支上提交至本地master分支,然后本地master提交至远程master分支 上.前提是远程只有一个mas ...

  4. PHP运行机制

    PHP运行机制: 访问html文件:访问的域名à解析成IP地址(hosts文件,dns)à找到服务器àapache拿到html文件à返回给浏览器à浏览器解析html内容 访问php文件:apache拿 ...

  5. .net程序集标示与绑定上下文

    之前在实现Autofac扫描自加载程序集实现IOC时候遇到程序集依赖的问题,在网上搜了一下,没有发现中文世界的相关描述.随取google拿了几篇文章,翻译&自己的理解,之后会写一些小demo, ...

  6. 配置Slf4j依赖,桥接各种多个日志组件(排除commons-logging依赖的影响)

    由于各个jar组件使用的日志框架不一样,实际项目中可能会引入多个jar,通常使用的日志框架有 commons-logging log4j 若同一个项目引入多个日志组件,那么Slf4j组件会有不能捕捉到 ...

  7. MySQL慢查询分析工具pt-query-digest详解

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  8. HTML三种样式引入方式

    HTML三种样式引入方式   HTML有三种样式引入方式:行内样式(inline Styles).嵌入式样式表(Embedded Style Sheets).外部样式表(External Style ...

  9. (生产)vuex - 状态管理

    参考:https://vuex.vuejs.org/zh-cn/ 安装 直接下载 / CDN 引用 https://unpkg.com/vuex在 Vue 之后引入 vuex 会进行自动安装:< ...

  10. Android 仿微信朋友圈拍小视频上传到服务器

    这个接上一个写的实现拍小视频和传到服务器的  界面是这个样子滴. 我也知不知道怎么给图片搞小一点o(╯□╰)o 布局文件是这样的[认真脸] <?xml version="1.0&quo ...