原题链接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/

题目大意:中序遍历二叉树

解题思路:中序遍历二叉树。中序遍历二叉树的左子树,訪问根结点,中序遍历二叉树的右子树。非递归实现时,用一个栈模拟遍历过程就可以。由于须要先遍历左子树。所以每一个结点先入栈。出栈时訪问。

vector<int> inorderTraversal(TreeNode *root) {
vector<int> ret;
if(!root)
return ret;
stack<TreeNode*> s;
while(root||!s.empty())
{
if(!root)
{
root=s.top();
ret.push_back(root->val);
s.pop();
root=root->right;
}
else
{
s.push(root);
root=root->left;
}
}

复杂度分析:时间复杂度为O(N),由于每一个结点仅遍历一次。

空间复杂度为O(lgN)。为栈的最大长度。即树深。

中序遍历和先许遍历一样须要熟练掌握,bug-free哦。

Binary Tree Inorder Traversal--leetcode的更多相关文章

  1. Binary Tree Inorder Traversal -- LeetCode 94

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

  2. Binary Tree Inorder Traversal ——LeetCode

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

  3. Binary Tree Inorder Traversal leetcode java

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

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

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

  5. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  6. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  7. 49. leetcode 94. Binary Tree Inorder Traversal

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

  8. LeetCode: Binary Tree Inorder Traversal 解题报告

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

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

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

  10. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

随机推荐

  1. java 线程 生产者-消费者与队列,任务间使用管道进行输入、输出 解说演示样例 --thinking java4

    package org.rui.thread.block2; import java.io.BufferedReader; import java.io.IOException; import jav ...

  2. HTTP Error 500.19

    HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related ...

  3. JDBC整理

    JDBC提供了独立于数据库的统一Api,用以执行SQL命令.JDBC API由以下常用的接口和类组成: DriverManagement:用于管理JDBC驱动的服务类,程序中使用该类的主要功能是获取C ...

  4. mysql(8.0.*版本 windows10 )忘记密码解决方案

    安装完mysql-8.0.13-winx64后,一些列的安装命令过后再执行mysql -uroot -p之后 报错了 what fuck 什么鬼,就是这个错 ERROR (): Access deni ...

  5. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  6. 2017.7.15清北夏令营精英班Day1解题报告

    成绩: 预计分数:20+10+40 实际分数:100+10+40. 一百三十多人的比赛全场rand7还水了个鼠标+键盘 unbelievable! 考试题目链接: https://www.luogu. ...

  7. Core Java(七)

    面向对象特性整理 知识点:一. static修饰符 static修饰符可以用来修饰类的成员变量.成员方法和代码块.            . 用static修饰的成员变量表示静态变量,可以直接通过类名 ...

  8. 多年js学习累计总结

    http://www.codesec.net/list/6/ 大神http://www.cnblogs.com/tylerdonet/p/5543813.html

  9. 03《UML大战需求分析》之三

    学习了活动图之后,我又学习了流程分析工具之二的状态机图.看上去状态机图和活动图很类似,我也很容易从活动图的角度来理解状态机图.但是学习之后,发现两种图是两种完全不同的分析角度.活动图在流程分析时是玩你 ...

  10. CGI与ISAPI的区别(转)

    一 CGI原理及其性能 1) CGI概念CGI即通用网关接口(Common Gateway Interface),它是一段程序,运行在服务器上,提供同客户端HTML页面的交互,通俗的讲CGI就象是一座 ...