Leetcode 之Binary Tree Inorder Traversal(43)

树的中序遍历。先不断压入左结点至末尾,再访问,再压入右结点。注意和先序遍历的比较
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> result;
stack<TreeNode *>s;
TreeNode *p = root;
while (!s.empty() || p != nullptr)
{
if (p != nullptr)
{
//将当前结点和其左结点不断入栈
s.push(p);
p = p->left;
}
else
{
//访问到了左结点末尾
//访问该结点并弹出
p = s.top();
result.push_back(p->val);
s.pop();
//将方向转为其右结点
p = p->right;
}
}
return result;
}
Leetcode 之Binary Tree Inorder Traversal(43)的更多相关文章
- [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
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- 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二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- BZOJ5319 & 洛谷4559 & LOJ2551:[JSOI2018]军训列队——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5319 https://www.luogu.org/problemnew/show/P4559 ht ...
- Eclipse NDK 打印LOG信息(都在jni目录下操作)
http://blog.csdn.net/u013045971/article/details/46448975 1 在.c文件中,引用头文件,定义TAG.LOG宏: #include <and ...
- ACE线程管理机制-并发控制(4)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581857.html ACE Synchronization类 这一类并发控制对象一般也 ...
- 【题解】回文串 APIO 2014 BZOJ 3676 COGS 1985 Manacher+后缀数组+二分
这题可以用回文自动机来做,但是我并没有学,于是用Manacher+SA的做法O(nlogn)水过 首先,看到回文串就能想到用Manacher 同样还是要利用Manacher能不重复不遗漏地枚举每个回文 ...
- C#学习之泛型功能与限制
在泛型类的描述中还会有时需要很多限制,例如对待一个泛型类型,在类中定义一个变量需要初始化时,不能确定是用Null还是0. 因为不能够确定它是值类型还是引用类型,这时可以用到default语句(下面有介 ...
- Long Parameter List(过长参数列)---要重构的味道
一个函数,它的参数过多是不好的,不好维护和修改,易读性也差,容易出错. 消除过长参数的方法,有如下: 1.在面向对象中,你可以传递一个对象给函数,函数通过访问对象来获得参 ...
- 省队集训 Day5 选举
[题目大意] 小奇和魔法猪要竞选膜钟国的总统. 有 $n$ 个选民,编号为$1...n$,他们中有的人支持小奇,有的人支持魔法猪,还有的人保持中立. 现在你需要把选民分成若干个区间,每个区间的长度在$ ...
- bzoj3043 IncDec Sequence
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3043 [题解] 比较神奇的一道题,开始没往差分的角度上想,所以没想出来. 考虑查分数组,有$ ...
- 【BZOJ】1754: [Usaco2005 qua]Bull Math
[算法]高精度乘法 #include<cstdio> #include<algorithm> #include<cstring> using namespace s ...
- 超详细的Java面试题总结(二)之Java基础知识篇
多线程和Java虚拟机 创建线程有几种不同的方式?你喜欢哪一种?为什么? 继承Thread类 实现Runnable接口 应用程序可以使用Executor框架来创建线程池 实现Callable接口. 我 ...