刷题94. Binary Tree Inorder Traversal
一、题目说明
题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列。题目难度是Medium!
二、我的解答
用递归遍历,学过数据结构的应该都可以实现。
class Solution{
public:
vector<int> inorderTraversal(TreeNode* root){
if(root != NULL){
if(root->left !=NULL)inorderTraversal(root->left);
res.push_back(root->val);
if(root->right !=NULL)inorderTraversal(root->right);
}
return res;
}
private:
vector<int> res;
};
Runtime: 4 ms, faster than 61.00% of C++ online submissions for Binary Tree Inorder Traversal.
Memory Usage: 10.5 MB, less than 5.00% of C++ online submissions for Binary Tree Inorder Traversal.
三、优化措施
用非递归算法,需要一个栈,代码如下:
class Solution{
public:
//iteratively
vector<int> inorderTraversal(TreeNode* root){
stack<TreeNode*> st;
TreeNode* p = root;
if(p != NULL){
while(p !=NULL) {
st.push(p);
p = p->left;
}
while(!st.empty()){
p = st.top();
st.pop();
res.push_back(p->val);
if(p->right !=NULL) {
p = p->right;
while(p !=NULL) {
st.push(p);
p = p->left;
}
}
}
}
return res;
}
private:
vector<int> res;
};
性能:
Runtime: 4 ms, faster than 60.93% of C++ online submissions for Binary Tree Inorder Traversal.
Memory Usage: 9.2 MB, less than 89.00% of C++ online submissions for Binary Tree Inorder Traversal.
刷题94. Binary Tree Inorder Traversal的更多相关文章
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 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 ...
- 【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:Giv ...
- 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
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 ...
随机推荐
- [bzoj4872] [洛谷P3750] [六省联考2017] 分手是祝愿
Description Zeit und Raum trennen dich und mich. 时空将你我分开. \(B\) 君在玩一个游戏,这个游戏由 \(n\) 个灯和 \(n\) 个开关组成, ...
- 【WPF学习】第十五章 WPF事件
前两章学习了WPF事件的工作原理,现在分析一下在代码中可以处理的各类事件.尽管每个元素都提供了许多事件,但最重要的事件通常包括以下5类: 生命周期事件:在元素被初始化.加载或卸载时发生这些事件. 鼠标 ...
- Sublime Text 3 部分安装过程记录
概览: Sublime Text 3下载网址 Package Control的安装 Install Package报错(There are no packages availabel for inst ...
- mong 的 安装 和测试
<hr>
- 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
结果呢: <a class="myCSSClass" href="/" id="myAnchorID">This is an o ...
- c#数字图像处理(八)图像平移
使图像沿水平方向和垂直方向移动 /// <summary> /// 图像平移 /// </summary> private void translation_Click(obj ...
- 数据结构 二维数组-->稀疏数组-->二维数组
稀疏数组基本概念: 稀疏数组应用场景: 当一个数组大部分的元素为"0",或者为同一个值的数组时,可以使用稀疏数组来保存该数组 处理方法: 1>记录数组一共有几行几列,有多少不 ...
- PowerCat DNS 隧道通信
powercat 也是一套基于 DNS 通信协议的工具.Powercat的dns的通信是基于dnscat设计的(其服务端就是dnscat).在使用dnscat时需要进行下载和编译. dnscat服务端 ...
- ASP.NET Core下Ocelot的简单使用
一.创建demo项目 1.新建webapi项目,命名为“DemoProject”,去掉HTTPS勾选 using Microsoft.AspNetCore.Mvc; using System.Coll ...
- JWT | io.jsonwebtoken.security.WeakKeyException: The signing key's size is 1024 bits which is not se
背景 今天集成JWT的时候,选用了PS256算法,在用使用PGP KEY作为私钥JWT进行签名的时候,报了如下错误: "C:\Program Files\Java\jdk1.8.0_161\ ...