非递归的中序遍历,要用到一个stack

class Solution {
public: vector<int> inorderTraversal(TreeNode* root) {
vector<int> ret;
if(!root)
return ret;
//a(ret)
stack<TreeNode*> stk;
stk.push(root);
//ahd(root)
//a(stk)
//dsp
TreeNode* p=root;
while(p->left){
stk.push(p->left);
p=p->left;
//dsp
} while(stk.size()>){
TreeNode* cur=stk.top(); stk.pop();
ret.push_back(cur->val);
//a(cur)
//lk("root", cur)
//dsp
if(cur->right){
stk.push(cur->right);
p=cur->right;
//dsp
while(p->left){
stk.push(p->left);
p=p->left;
//dsp
}
}
}
return ret;
}
};

程序动态运行结果: http://simpledsp.com/FS/Html/lc94.html

LeetCode 94. Binary Tree Inorder Traversal 动态演示的更多相关文章

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

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

  2. 49. leetcode 94. Binary Tree Inorder Traversal

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

  3. Leetcode 94. Binary Tree Inorder Traversal

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

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

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

  5. Java [Leetcode 94]Binary Tree Inorder Traversal

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

  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 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

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

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

随机推荐

  1. 如何利用scrapy新建爬虫项目

    抓取豆瓣top250电影数据,并将数据保存为csv.json和存储到monogo数据库中,目标站点:https://movie.douban.com/top250 一.新建项目 打开cmd命令窗口,输 ...

  2. RMQ 区间最大值最小值 最频繁次数

    区间的最大值和最小值 #include <cstdio> #include <cstring> #include <cmath> #include <iost ...

  3. VS2017报错:未提供初始值设定项

    今天在使用VS2017写程序时,报错: 出错的代码如下: #include "pch.h" #include <iostream> #include <threa ...

  4. CSP-J&S 游记以及总结

    前言 以前的比赛没有写什么总结,都只注重结果,没有注重过程,不知道如何才能在比赛中拿到高分,因此这次CSP才会考得一塌糊涂. 从此吸取教训,每场比赛都要总结经验,每场比赛都要尽全力考,每个题目都要尽全 ...

  5. zookeeper之四 Curator客户端的使用

    Curator是一个开源的zookeeper客户端,解决了很多zookeeper原生客户端非常底层的细节开发工作,如连接重试.反复注册watcher等. public class CuratorOpe ...

  6. Laravel——缓存使用

    1.使用Redis类 use Illuminate\Support\Facades\Redis; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  7. CSP2019 题解

    CSP2019 题解 D1T1 格雷码(code) 题目传送门 https://loj.ac/problem/3208 题解 按照题意模拟就可以了. 对于第 \(i\) 位,如果 \(k \geq 2 ...

  8. 与Swing的相识

    参考自http://c.biancheng.net/swing/ Swing是一个用于Java GUI编程(图形界面设计)的工具包(类库):换句话说,java可以用来开发带界面的PC软件,使用到的工具 ...

  9. JavaSE---显式锁

    1.概述 1.1.jdk5之前,用于  调节共享对象访问机制  只有 synchronized.volatile:     jdk5之后,提供了  显示锁:Lock.ReentrantLock...: ...

  10. SpringCloud学习系列-Eureka服务注册与发现(4)

    actuator与注册微服务信息完善 1.主机名称:服务名称修改 当前问题 含有主机名称 修改修改microservicecloud-provider-dept-8001 的yml文件 修改内容 eu ...