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 ...
随机推荐
- [转]将input file的选择的文件清空
本文转自:http://hi.baidu.com/xiongshihu/item/125c79b47632e794194697f5 上传文件时,选择了文件后想清空文件路径的两种办法: JS代码 < ...
- thinkphp3.2 删除Runtime目录里的缓存文件,标记一下,以下好找。
操作如下: utility::clearCache("Data"); 或 utility::clearCache("Data-Logs"); class uti ...
- git跟svn 服务端对比
Git已经火了很久,简单的使用也没有问题,但有几个问题一直以来都没有搞清楚:git跟svn有哪些异同,两者相互的优劣是什么,git的分布式怎么理解,为什么有离线提交,,,自己动手,分别看一下服务端跟客 ...
- 用一层for循环初始化三维数组
][][]; ; i < * * ; i++) { a[i / ][(i / ) % ][i % ] = i; printf(, (i / ) % , i % ); // printf(&quo ...
- ruby逻辑判断符号
puts true and false #相当于 (puts true) and false Use &&/|| for boolean expressions, and/or fo ...
- Devexpress Xtrareport 创建主从报表
效果 xtrareport 布局 From 代码 private DataSet Getdata() { DataSet ds = new DataSet(); //config配置字符串 strin ...
- Oracle 数据库 导入导出空表解决办法!
expdp导出:(打开CMD) 先创建(任意盘符):\oracle_data 文件夹 1.sqlplus / as sysdba;2.create or replace directory d_nam ...
- php一种面向对象的语言,那么什么是面向对象呢?
php一种面向对象的语言,那么什么是面向对象呢? 传统的面向过程的编程思想: 相信很多人第一次接触编程都是c语言,c语言就是非常典型的面向过程的编程语言,将要实现的功能描述为一个从开始到结束的连续的“ ...
- vue中 eCharts 自适应容器
在 vue 脚手架开发中,echarts图表自适应容器的方法: 父组件: <template> <div class="statistics_wrap"> ...
- 移动web基础
接触retina屏 基础知识(移动Web的基础知识)排版布局(高效的移动Web布局)开发效率终端交互优化 pixel像素基础viewport视图viewport_meta标签viewport_codi ...