LC 94. Binary Tree Inorder Traversal
问题描述
Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右)
Example:
Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
参考答案
/**
* 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) {
// init
vector<int> res;
stack<TreeNode* > st;
TreeNode* p = root; // 初始化根节点 while(p||!st.empty()){
// 一旦遇到节点,先考虑左边的,直到尽头,如果没有之后的右node,就停止运行了
while(p){
st.push(p);
p = p->left;
}
// 立刻提取p的信息,并且把p弹出来。如果进入while,那么这一步只会是左孩子,如果没进入while,那么会是父节点/右节点。
p = st.top();
st.pop();
res.push_back(p->val);
// 把p 变成p的右孩子
p = p->right;
}
return res;
}
};
LC 94. Binary Tree Inorder Traversal的更多相关文章
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 【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
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 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 ...
随机推荐
- CF1217B
CF1217B 题意: 有一个有 $ x $ 个头的龙,你有 $ n $ 种方案,每种方案中包含你可以砍掉的头 $ d_i $ 和龙会生长的头 $ h_i $ 找到一种方案,使得操作数最少. 解法: ...
- Wondershare Video Converter Ultimate 注册码 License
这个软件可以直接将DVD iso转换成mp4播放,可以破解,还不错,特地记录 官网下载最新版: https://videoconverter.wondershare.com/gl/video-conv ...
- Yarn 安装 node-sass 依赖导致 Build Fresh Packages 太慢的问题
解决办法: 1. 在 项目目录下新建 .yarnrc 文件 添加以下代码 registry "https://registry.npm.taobao.org" sass_binar ...
- python selenium 的配置安装
selenium的使用需要以下几个配置步骤. (1) 首先安装selenium,使用python自带的pip进行安装.若pip配置到系统环境变量,可以直接在cmd命令行中使用,若没有配置到到环境变量, ...
- docker 命令 记录
获取指定容器的ip docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_nam ...
- tocke - 加密
//获取32位随机字符串 $randTocke = getRandChar($length); //当前脚本运行时间, 单位为十万分之一毫秒 $timestamp = $_SERVER['REQUES ...
- vi下如何配置自动更新标签(tags)?
答: 在~/.vimrc中写入以下内容即可: au BufWritePost *.c,*.cpp,*.h silent! !ctags -R & 参考资料: 一键打造vim ide
- Java 实现 bash命令
Java 实现 bash命令 1.BASH 命令简介 Bash,Unix shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写.1989年发布第一个正式版本,原先是计划用在GNU操作系统 ...
- k8s学习之资料参考网址(持续更新)
此文章持续更新关于学习k8s生态的参考网址: 二进制方式搭建 (此部署方式是一步一步的部署,过程清晰) https://github.com/opsnull/follow-me ...
- 超详细的EM算法理解
众所周知,极大似然估计是一种应用很广泛的参数估计方法.例如我手头有一些东北人的身高的数据,又知道身高的概率模型是高斯分布,那么利用极大化似然函数的方法可以估计出高斯分布的两个参数,均值和方差.这个方法 ...