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 ...
随机推荐
- 【LDAP】LDAP介绍
原文:http://ldapman.org/articles/intro_to_ldap.html原文作者:Michael Donnelly 什么是LDAP? LDAP的英文全称是Lightweigh ...
- XStream xml转java对象2
<?xml version="1.0" encoding="UTF-8" ?> <person> <name>yunyun& ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- Unable to run man pages on Centos 6
I just installed CentOS 6 with minimal install. When i tried to read the linux manual pages using ma ...
- webpack打包将配置文件单独抽离不压缩打包
webpack.config.js: plugins: [ //提取公共模块 new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', ch ...
- Vue-Cli 3 引入 SCSS 全局变量
首先创建一个全局变量文件 global.scss $theme-color: #efefef; 编辑vue.config.js module.exports = { // ... css: { loa ...
- Inconsistent accessibility
Inconsistent accessibility: return type 'ConsoleApplication17.IBacklight' is less accessible than me ...
- Python开发环境Wing IDE的Blender的Python代码调试技巧
Wing IDE是一个集成开发环境,可用于开发.测试和调试为Blender编写的Python代码,Blender是一个开源的3 D内容创建系统.Wing IDE提供自动完成.调用提示.强大的调试器.以 ...
- python 生成随机图片验证码
1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...
- sharepoint2010列表的分页实现迅雷样式效果
利用ListItemCollectionPosition和AspNetPage分页控件实现,效果图如下: 后台分页代码如下: #region 私有方法 /// <summary> /// ...