先序遍历的非递归办法,还是要用到一个stack

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

程序运行动态演示:http://simpledsp.com/FS/Html/lc144.html

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

  1. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  2. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  3. Java for LeetCode 144 Binary Tree Preorder Traversal

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

  4. leetcode 144. Binary Tree Preorder Traversal ----- java

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

  5. Java [Leetcode 144]Binary Tree Preorder Traversal

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

  6. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

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

  7. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

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

  8. Leetcode 144 Binary Tree Preorder Traversal 二叉树

    二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...

  9. LeetCode 94. Binary Tree Inorder Traversal 动态演示

    非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...

随机推荐

  1. 分支结构 :if - else

    分支结构 :if - else 格式一: if(条件表达式){ 执行语句; } 格式二:二选一 if(条件表达式){ 执行语句1; }else{ 执行语句2; } 格式三: 多选一 if(条件表达式1 ...

  2. 中标麒麟系统安装rpm文件

    打开终端,获得su权限. cd到rpm所在文件夹,输入指令,rpm -ivh rpm的名称

  3. 2019牛客暑期多校训练营(第三场) - J - LRU management - 模拟

    https://ac.nowcoder.com/acm/contest/883/J 根据这个数据结构的特点,也就是计算机组成原理里面学过的cache的LRU管理算法,每次访问都会在cache中查询一页 ...

  4. RMAN备份与恢复 —— 完全恢复与不完全恢复

    名词解释: 顾名思义,完全恢复就是指数据没有丢失的恢复了.不完全恢复是指恢复后有部分数据丢失.它们是数据库的两种恢复方式.        完全恢复:利用重做日志或增量备份将数据块恢复到最接近当前时间的 ...

  5. SpringDataJPA使用

    一.简介 SpringDataJpa是 JPA规范的一个很好的实现,简化了开发的复杂度,极大提升了开发的效率.SpringDataJpa通过 Repository接口及子接口可以很方便的实现持久化操作 ...

  6. LeetCode102. 二叉树的层次遍历

    102. 二叉树的层次遍历 描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 示例 例如,给定二叉树: [3,9,20,null,null,15,7], 3 / ...

  7. 计算机体系结构——流水线技术(Pipelining)

    本文导读: 一.并行技术 .并行技术分类 .新技术的设计与实现 .指令周期 二.流水线技术 .什么是流水线 .指令重叠方式 .流水工作设计 .流水线的描述方法(时空图) .流水线特点 三.流水线的分类 ...

  8. postgresql Streaming Replication监控与注意事项

    一监控Streaming Replication集群 1 pg_stat_replication视图(主库端执行) pid Wal sender process的进程ID usesysid 执行流复制 ...

  9. tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

      windows系统: 部署了一个Tomcat8.5.15,bin目录下startup.bat执行,结果提示Neither the JAVA_HOME nor the JRE_HOME enviro ...

  10. lsattr 查看文件扩展属性

    1. 命令功能 lsattr查看 是否有chattr设置的权限. 2. 使用范例 [root@localhost data]# lsattr resolv.conf -----a-------e- r ...