一、题目说明

题目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的更多相关文章

  1. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  2. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  3. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  4. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  5. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  6. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  7. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  9. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. gitbub 基本使用

    一.环境 git:https://git-scm.com/ 申请github账号:https://github.com/ 二.安装git 一直next即可 三.创储存建库 1.选择New reposi ...

  2. Java入门 - 高级教程 - 04.序列化

    原文地址:http://www.work100.net/training/java-serialization.html 更多教程:光束云 - 免费课程 序列化 序号 文内章节 视频 1 概述 2 序 ...

  3. 高通量计算框架HTCondor(二)——环境配置

    目录 1. 概述 2. 安装 3. 结果 4. 相关 1. 概述 HTCondor是开源跨平台的分布式计算框架,在其官网上直接提供了源代码和Windows.Linux以及MacOS的安装包.因为平台限 ...

  4. idea初使用之自动编译

    原文地址:https://blog.csdn.net/diaomeng11/article/details/73826564/ 因为公司需要,方便使用框架以及代码整合,使用同一开发集成环境idea,因 ...

  5. 超越队西柚考勤系统--Beta冲刺2

    超越队西柚考勤系统- -beta冲刺2 格式描述 这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业链接 团队名称 超越队(西柚考勤系统) 这个作业的目标 对alpha版本的程序进行改进,代码核 ...

  6. HDFS NameNode重启优化

    http://tech.meituan.com/namenode-restart-optimization.html 一.背景 在Hadoop集群整个生命周期里,由于调整参数.Patch.升级等多种场 ...

  7. learn more ,study less(二):整体性学习技术(上)

    前言:在学习整体性学习概念时,一个很好的方法是把它比喻成下棋,首先你要了解下棋的 基本规则和基本目标,本书第一部分可以看做是介绍关于整体性学习的一整套规则和目标. 一旦你理解了下棋的基本规则,你就要开 ...

  8. Ubuntu通过终端命令行换阿里源

    检查Ubuntu系统的Codename $ lsb_release -a 得到结果: No LSB modules are available. Distributor ID: Ubuntu Desc ...

  9. 10个很多人不知道的Redis使用技巧

    前言 Redis 在当前的技术社区里是非常热门的.从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redis已经走过了很长的一段路.随之而来的一系列最佳实践,使得大多数人可以正 ...

  10. 并发队列之LinkedBlockingQueue

    上一篇我们看了一下这个队列ConcurrentLinkedQueue,那就是一个无界非阻塞链表,我们这次来看看LinkedBlockingQueue,这个队列看名字就知道是一个阻塞式队列(也就是一个单 ...