算法描述:

Given a binary tree, return the preorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]1\2/3Output: [1,2,3]

Follow up: Recursive solution is trivial, could you do it iteratively?

解题思路:先根非递归遍历。

    vector<int> preorderTraversal(TreeNode* root) {vector<int> results;stack<TreeNode*> stk;while(root!=nullptr || !stk.empty()){while(root!=nullptr){stk.push(root);results.push_back(root->val);root=root->left;}TreeNode* temp = stk.top();stk.pop();if(temp->right!=nullptr)root = temp->right;}return results;}

转载于:https://www.cnblogs.com/nobodywang/p/10353873.html

LeetCode-144-Binary Tree Preorder Traversal相关推荐

  1. 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal

    LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...

  2. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal

    66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...

  3. 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 (层次遍历)

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

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

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

  6. [LeetCode] 144. Binary Tree Preorder Traversal Java

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

  7. LeetCode 144. Binary Tree Preorder Traversal--二叉树前序遍历--反向压栈--迭代-栈,递归--C++,Python解法

    题目地址:Binary Tree Preorder Traversal - LeetCode Given a binary tree, return the preorder traversal of ...

  8. [LeetCode 题解]: Binary Tree Preorder Traversal

    前言 [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...

  9. 144. Binary Tree Preorder Traversal(非递归实现二叉树的前序遍历)

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

  10. 栈和递归的关系 144:Binary Tree Preorder Traversal

    前序遍历:根左右 //用栈来实现非递归解法/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode ...

最新文章

  1. Hive-1.2.0学习笔记(一)安装配置
  2. Java范例集锦(一)
  3. php如何防止消息被篡改,php如何用libevent处理rabbitmq发来的消息,防止消息丢失或者人为的中断导致消息没有被处理完整...
  4. octave修改选中的代码行的颜色
  5. VMware10出现VMware Workstation 不可恢复错误: (vmx)
  6. Git 二分调试法,火速定位疑难Bug!
  7. 佳能g2800加墨水后发生打印机错误5b00_备注!淳安县联想LJ4000D黑白激光打印机销售,县联想M7400Pro打印机长租...
  8. Python yolo 神经网络实现视频中的行人车辆检测计数(车流量统计)YOLO多目标跟踪与计数
  9. 计算机械效率的公式四种,初级中学物理计算公式定律整编汇总.doc
  10. 大概的知识点 有点乱(乱的一批)
  11. eWebEditor的使用
  12. Clickhouse时间日期函数一文详解+代码展示
  13. 搬运--domoticz的 MQTT API接口 函数英文
  14. “POW'ER 2020 DEFI 创新者大会” | Conflux CTO 伍鸣畅谈公链如何成为 DeFi 的完美世界...
  15. 成对数据T检验实例(用SPSS16.0实现)
  16. ZYNQ-7的芯片引脚
  17. attempt包测试3_Adverbs_2020-02-04
  18. if与switch的性能比较
  19. 希尔伯特变换、解析信号与复包络
  20. es文件服务器4.0,全新安卓5.0设计!ES文件浏览器4.0体验

热门文章

  1. python中的reindex_Python reindex使用详解
  2. ac9560不支持承载_工程人还在为松软土层地质问题处理而烦恼?不,这里告诉你!...
  3. pythontuple数据类型_Python数据类型之Tuple元组实例详解
  4. java expression 强制出现_Java中带有强制括号对的单行循环
  5. php 网络图片 execl,phpexcel图片获取
  6. python dataframe 选取字段 特别慢_从parqu读取dask dataframe列重命名速度较慢(er)
  7. webbrowser控件 加载为空白_深入学习SAP UI5框架代码系列之一:UI5 Module的懒加载机制...
  8. 多项目加载顺序修改_面试官:Java 类在 Tomcat 中是如何加载的?
  9. 怎么用百度搜索php网站,PHP简单获取网站百度搜索和搜狗搜索收录量的方法
  10. ASP.NET的ASHX一般处理程序中Session为null且不能赋值