非递归的中序遍历,要用到一个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. lspci - 列出所有PCI设备

    总览 SYNOPSIS lspci [options] 描述 DESCRIPTION lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 为了能使用这个命令所有功能, ...

  2. Xor Sum 2 AtCoder - 4142 (异或前缀和性质+ 双指针)

    Problem Statement There is an integer sequence A of length N. Find the number of the pairs of intege ...

  3. blazeFace

    围绕四个点构造模型 1.扩大感受野 使用5*5卷积替换3*3来扩大感受野,在深度分离卷积中,pw与dw计算比为d/k^2,d为输出通道,k为 dw的卷积核,即增加dw的卷积核所带来的计算并不大. 在M ...

  4. Codeforces 959 树构造 暴力求最小字典序互质序列

    A B C 题目给你一个结论 最少需要min((odd,even)个结点可以把一棵树的全部边连起来 要求你输出两颗树 一棵树结论是正确的 另外一棵结论是正确的 正确结论的树很好造 主要是错误的树 题目 ...

  5. linux shell 命令笔记

    标准输入.标准输出.标准错误 File descriptors are integers associated with an opened file or data stream. File des ...

  6. java:序列化Serializable 接口

    java:序列化Serializable 接口 public class SerializePerson implements Serializable { private String name; ...

  7. angularJS拖动marker时popup一直显示

    $scope.$on('leafletDirectiveMarker.drag', function(event, arg) { arg.leafletObject.openPopup(); });

  8. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  9. Java——super

    在Java类中使用super来引用基类的成分.   [代码]  

  10. Liunx的软链接和硬链接

    ln 命令  命令名称: ln. 英文原意: make links between file. 所在路径: /bin/ln. 执行权限:所有用户. 功能描述:在文件之间建立链接. ln 命令 ...