先序遍历的非递归办法,还是要用到一个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. Eclipse打包可执行jar包操作步骤

    1.右键点击工程,选择Export…,进入页面 2.弹出对话框,选择Java->Runnable JAR file ,点击Next>,页面显示jar包的输出路径,配置为jmeter的/li ...

  2. css炫酷动画收藏

    1.按钮.hover.input动画(cssfx) https://cssfx.dev/ 2.svg 矢量定制icon(ikonate) https://www.ikonate.com/#conten ...

  3. 10.Linux-CentOS系统重启之后Xshell无法SSH连接(云环境)

    问题:云环境下CentOS系统断电或强制关机,再开机出现问题:Entering emergency mode. Exit the shell to continue. Generating " ...

  4. 阿里P8技术栈

  5. iconv 转换文件的编码格式

    1.命令功能 icnov用于转换文件的编码格式 linux默认中没有icnov文件,需要自己安装http://www.gnu.org/software/libiconv/. (1)下载libiconv ...

  6. [Luogu1436]棋盘分割(动态规划)

    [Luogu1436]棋盘分割 题目背景 无 题目描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的两部分中的任意一块继续如此分割,这样割了(n-1)次后, ...

  7. rabbitmq tags

    #用户角色####################### RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator Ra ...

  8. lvm分区创建和扩容

    shell> fdisk /dev/xvdb #### 选择磁盘 Command (m for help): m #### 帮助 Command action a toggle a bootab ...

  9. bzoj3991 [SDOI2015]寻宝游戏 树链的并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3991 题解 貌似这个东西叫做树链的并,以前貌似写过一个类似的用来动态维护虚树. 大概就是最终的 ...

  10. Centos7.5中的SElinux操作命令说明

    设置Selinux模式 setenforce 0 0表示警告模式 1表示强制模式 关闭要设置/etc/sysconfig/selinux下将"SELINUX=enforcing"改 ...