一、题目说明

题目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. [bzoj4569] [loj#2014] [Scoi2016] 萌萌哒

    Description 一个长度为 \(n\) 的大数,用 \(S1S2S3...Sn\) 表示,其中 \(Si\) 表示数的第 \(i\) 位, \(S1\) 是数的最高位,告诉你一些限制条件,每个 ...

  2. python接口自动化测试 - unittest框架suite、runner详细使用

    test suite 测试套件,理解成测试用例集 一系列的测试用例,或测试套件,理解成测试用例的集合和测试套件的集合 当运行测试套件时,则运行里面添加的所有测试用例 test runner 测试运行器 ...

  3. Linux起源

    Linux起源 操作系统出现时间线: Unix1970年诞生 ,71年用C语言重写 Apple II 诞生于1976年 window诞生于1985年 Linux诞生于1991年,由大学生Linus T ...

  4. 夜晚 十点 React-Native 源码 暴力畜 系列

    百度 上 给的 关于 React-Native 的 排名 前三 继续 跟

  5. 学习 lind layerdiagram 第三弹

  6. Spring Boot定义系统启动任务的两种方式

    Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...

  7. k8s概述

    k8s概述 概述 Kubernetes 使你在数以千计的电脑节点上运行软件时就像所有这些节点是单个大节点一样.它将底层基础设施抽象,这样做同时简化了应用的开发.部署, 以及对开发和运维团队的管理. K ...

  8. Web自动化测试项目(二)BasePage实现

    一.BasePage介绍 创建一个BasePage类,对Selenium Api进行二次封装 为了快速创建项目并投产,用到的Selenium Api才进行封装,没用到的则不封装 优先封装最重要的几个方 ...

  9. Docker基础内容之命令大全

    run(未补全) 说明:创建一个新的容器并运行一个命令 语法如下: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 选项说明: -a stdin: 指定标准 ...

  10. 找不到文件异常java.io.IOException: Resource [classpath:shiro.ini] could not be found.

    情景 tomcat启东时,老是报错,在classpath下找不到配置文件,但是配置文件已经放在resource目录下了 解决方案 出现该异常的原因,是因为新建的conf文件夹,识别不了,因为没有设置成 ...