非递归的中序遍历,要用到一个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. 四、VLC搭建rtsp服务器

    一.VLC搭建rtsp服务器 1.rtsp服务搭建 https://blz-videos.nosdn.127.net/1/OverWatch/AnimatedShots/Overwatch_Anima ...

  2. 【转】UBOOT——启动内核

    转自:https://www.cnblogs.com/biaohc/p/6403863.html 1:什么是UBOOT,为什么要有UBOOT? UBOOT的主要作用是用来启动linux内核,因为CPU ...

  3. vecto容器中一些没有注意到的地方

    vector容器 vectoor是一个单口容器. vector动态增长的基本原理 当插入新元素的时候,如果空间不足,那么vector会重新申请更大的一块内存空间,将原空间数据拷贝到新空间,释放旧空间的 ...

  4. python 编码解码

    一种编码想要转成另一种编码,需要先解码成万国码:Unicode,然后再从Unicode转成其他编码. 例如GBK格式想要转成utf-8,需要先按照 gbk 的格式 decode 成 unicode,再 ...

  5. python基础练习题2

    01:python九九乘法表 for i in range(1,10): for j in range(1,i+1): print('{}*{}={}'.format(j,i,i*j),end='\t ...

  6. 构建游戏开发的大数据项目的流程demo图

  7. 作用域变量 var

    var没有块级作用域,定义后在当前闭包中都可以访问,如果变量名重复,就会覆盖前面定义的变量,并且也有可能被其他人更改. 变量名重复,就会覆盖前面定义的变量,并且也有可能被其他人更改: console. ...

  8. Selenium Java tutorial

     https://eyes.applitools.com/app/test-results 1.

  9. git概述(三)

    Bug分支: 当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101来修复它,但是,等等,当前正在dev上进行的工作还没有提交: 并不是你不想提交,而是工作只进行 ...

  10. mongodb replica set搭建

    1/安装mongodb 配置repo: [mongodb-org-3.4]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/red ...