先序遍历的非递归办法,还是要用到一个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. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  2. java 多态 (知识点不完整)

    1.多态的条件 1.要有继承 2.方法重写 3.父类引用指向子类对象 多态中成员访问 1.   访问成员变量:  编译看左边,运行也看左边 f.num = 10 因为这里是父类的,看是父类Father ...

  3. 5105 pa1 MapReduce

    Programming Assignment 1: A simple MapReduce-like compute framework Yuanli Wang  wang8662            ...

  4. ES6——面向对象应用

    面向对象应用——React 特点:     1.组件化(模块化) --- class(一个组件就是一个class)     2.强依赖与JSX (JSX==babel==browser.js  是JS ...

  5. express通过生成器

    express通过生成器 [ 脚手架 ] 1. 作用:可以帮助快速构建一个express项目 2. 脚手架的安装 全局安装 [可以使用npm cnpm] $ cnpm i express-genera ...

  6. IE9的兼容性

    /* 解决IE9表格错位 */ .el-table--border th:last-of-type.gutter { display: table-cell !important; width: 50 ...

  7. Docker备份镜像

    docker save -o mycentos.tar mycentos_new:1.1 指定输出到的文件 执行后,运行 ls 命令即可看到打成的tar包, 因为有463M所以打包要一会

  8. 一条sql引发的“血案”

    前几天有一个项目要上线,需要对表的一个字段进行扩充,项目经理让我准备脚本,于是我准备了如下的脚本: )); )); )); 结果上线的时候,ord_log1和ord_log2表中有30万数据,在执行的 ...

  9. javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  10. openprocess打不开 如何读取exe路径描述

    openprocess打不开 如何读取exe路径描述 openprocess打不开 如何读取exe路径描述 https://bbs.pediy.com/thread-210652.htm https: ...