先序遍历的非递归办法,还是要用到一个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. 搜索(DFS)---查找最大连通面积

    查找最大的连通面积 695. Max Area of Island (Medium) [[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0] ...

  2. 修改url,

    第一种场景: 无论url怎么变,表单里面的url始终不变 http://127.0.0.1:8000/CC/indexssssssssssssssssss/ url(r'^indexsssssssss ...

  3. C#设计模式:外观模式(Facade Pattern)

    一,什么是外观模式? 外观模式:为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用. 二,我们看看代码的实现 using System; using Syst ...

  4. ASP.NET Core 2.2 : 二十六. 应用JWT进行用户认证及Token的刷新

    来源:https://www.cnblogs.com/FlyLolo/p/ASPNETCore2_26.html 本文将通过实际的例子来演示如何在ASP.NET Core中应用JWT进行用户认证以及T ...

  5. React框架新闻网站学习过程中遇到的错误总结

    1.安装指定版本插件命令 npm install 插件名字@1.1.4(版本号) --save 或 yarn add 插件名字@1.1.4(版本号) --dev 2.Error:‘Link’ is n ...

  6. Nginx优化总结

    目录 Nginx性能优化概述 一. 压力测试工具实战 二.了解影响性能指标 三.系统性能优化 四.静态资源优化 Nginx性能优化概述 基于Nginx性能优化,那么在性能优化这一章,我们将分为如下几个 ...

  7. 12JDBC

    1.JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java ...

  8. Ubuntu16.04 启用root权限

    装了Ubuntu 16.04之后想使用超级权限对系统进行操作 使用命令 su - 切换超级用户,提示输入密码,却怎么都不对,网上找的资料说是没有启用root权限,于是根据网上提供的方法启用root权限 ...

  9. 客户端模拟线程线程池发送100个文件给socket

    1.线程池模拟发送100个线程发送 2.每个线程启动一个socket发送文件 3.线程池最大并发几个

  10. 项目部署到tomcat上

    1:先讲解一下tomcat的各个目录的作用 2:将项目打包成war的格式,然后放到webapps chengtai   是启动项目的时候自动解压的,不需要我们手动解压. 3:启动tomcat 进入到b ...