leetcode笔记(二)94. Binary Tree Inorder Traversal
- 题目描述 (原题目链接)
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的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 【一天一道LeetCode】#94. Binary Tree Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
随机推荐
- WEB服务器、网站、域名、IP地址、DNS服务器之间的关系
域名首先指向你的服务器,这个过程叫解析. 服务器分成好多小块,每小块叫一个空间或者一个虚拟主机. 所以当你输入你的域名以后,服务器收到你域名的访问信息,但不知道要打开这么多个小块中的那一个.所以要 ...
- mybatis连接mysql数据库实现的jdbc功能
最近公司项目要使用myBatis,自己以前没有接触过,就在网上找到了一些资料研究了些.初步做出了基于myBatis连接mysql数据库的jdbc实现的功能. employee.java package ...
- windows中的ubuntu
摘要 windows的cmd 实在是太难用,对于码农来说还是喜欢linux的命令行,功能强大:但是linux下的办公软件又没有windows下的强大,在windows下安装个虚拟机吧太卡了: 所以出了 ...
- JavaScript判断变量类型
使用JavaScript变量时是无法判断出一个变量是0 还是“”的 这时可用typeof()来判断变量是string 还是number来区分0和“”, typeof(undefined) == 'un ...
- xml解析demo使用
package lianxi; import java.io.FileOutputStream;import java.io.OutputStreamWriter; import javax.xml. ...
- Html+CSS二周目--->常用概念
学习css几乎俩周,来总结一下 对于初学者来说,有一些基本的概念是我们应当清楚的.掌握这些概念,可以帮助你更加有效的开发,大大提高开发效率. 1.盒子模型 2.浮动(float) 3.定位(posit ...
- u-boot分析(十)----堆栈设置|代码拷贝|完成BL1阶段
u-boot分析(十) 上篇博文我们按照210的启动流程,分析到了初始化nand flash,由于接下来的关闭ABB比较简单所以跳过,所以我们今天按照u-boot的启动流程继续进行分析. 今天我们会用 ...
- 如何用C# 动态创建Access数据库和表?
记得以前要动态的创建Access数据库的mdb文件都是采用DAO,用VC开发,一大堆的API,很是麻烦.而且以前工作中需要全新的access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新 ...
- meta详解(常用)
1.<meta http-equiv="X-UA-Compatible" content="IE=edge"> 说明:设置浏览器的兼容模式版本.表示 ...
- 94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...