LeetCode 144. Binary Tree Preorder Traversal 动态演示
先序遍历的非递归办法,还是要用到一个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 动态演示的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 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 ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- LeetCode 94. Binary Tree Inorder Traversal 动态演示
非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...
随机推荐
- JVM-堆内存
1. java堆内存介绍 java的堆内存可以类比于计算机的内存,是存储整个机器数据的地方. (1)jvm一起动就创建java堆.类比计算机一起动就加载内存. (2)所有的线程共享.类比计算机所有进程 ...
- 新接口注册LED字符驱动设备
#include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...
- 如何在CentOS 7上安装Yarn
Yarn是与npm兼容的JavaScript软件包管理器,可帮助自动化安装,更新,配置和删除npm软件包的过程. 它的创建是为了解决npm的一系列问题,例如通过并行化操作并减少与网络连接有关的错误来加 ...
- 让Elasticsearch飞起来!——性能优化实践干货
原文:让Elasticsearch飞起来!--性能优化实践干货 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...
- spark复习笔记(6):数据倾斜
一.数据倾斜 spark数据倾斜,map阶段对key进行重新划分.大量的数据在经过hash计算之后,进入到相同的分区中,zao
- 使用Python和AWK两种方式实现文本处理的长拼接案例
最近由于业务系统新需求的需要,我们平台需要将供应商G提供一类数据转换格式后提供给客户K.比较头疼是供应商G提供的数据都是在Windows下使用Excel存储的,而客户K先前与我们相关对接人员商定的数据 ...
- 03机器学习实战之决策树scikit-learn实现
sklearn.tree.DecisionTreeClassifier 基于 scikit-learn 的决策树分类模型 DecisionTreeClassifier 进行的分类运算 http://s ...
- Linux shell中自动完成登录
在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...
- SpringBoot 利用freemaker生成静态页面
1. <!-- freemarker模板 --> <dependency> <groupId>org.springframework.boot</groupI ...
- $nextTick
Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 $nextT ...