非递归的中序遍历,要用到一个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. wpf textblock 长文本滚动

    在textblock添加滚动条 <ScrollViewer VerticalScrollBarVisibility="Auto"> <TextBlock x:Na ...

  2. mysql的innodb 引擎 表锁与行锁

    innodb 引擎 行锁与表锁 行锁与表锁是基于索引来说的(且索引要生效) 不带索引 (表锁)要全表扫描 1. 执行select @@autocommit; 查看结果 0是不自动提交事务,1是自动提交 ...

  3. 使用idea实现SSM框架整合

    SM框架整合 1      使用idea创建一个maven webapp项目 到此为止项目初步建立,需要等待maven对项目结构进行组织,直到状态栏的进度条完成,且项目的目录结构如下: 2      ...

  4. Codeforces Round #427 (Div. 2) - D

    题目链接:http://codeforces.com/contest/835/problem/D 题意:给定一个字符串,定义kth回文是左半部分等于右半部分,并且左半部分和右半部分都是(k-1)th回 ...

  5. 下载zip

  6. NNIE(待尝试)

    马克 https://blog.csdn.net/ywcpig/article/details/85260752 https://blog.csdn.net/u011728480/article/de ...

  7. qt02 textEdit

    1.向QTextEdit中当前光标位置添加一行字符串message ui.messageTextEdit->textCursor().insertText(message+"\n&qu ...

  8. snmpwalk工具使用

     snmpwalk是SNMP的一个工具,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. 在linux下使用snmpwalk工具,我们 ...

  9. Web防止button按钮点击多次

    BtnPass.Attributes.Add("onclick", "javascript:if(document.getElementById('IsSubmited' ...

  10. Nginx动静分离-tomcat

    一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...