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?

方法一:递归

思路很简单,根左右

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public void preorderTraversal(TreeNode root) {if(root==null) return ;System.out.print(root.val+' ');preorderTraversal(root.left);preorderTraversal(root.right);}
}

方法二:迭代

所有用递归的题都能用迭代解,递归无非是利用系统的函数栈,如果自己申请数据结构来代替函数栈,也能实现相同功能。

前序遍历是根左右,根先加入栈中,再弹出,每次弹出时,将弹出结点的右左子树加入,保证弹出的顺序是根左右。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public List<Integer> preorderTraversal(TreeNode root) {if(root==null) return null;if(root!=null) {Stack<TreeNode> stack=new Stack<TreeNode>();List<Integer> list=new ArrayList<Integer>();stack.add(root);while (!stack.isEmpty()){root=stack.pop();list.add(root.val);if(root.right!=null) {stack.add(root.right);}if(root.left!=null){stack.add(root.left);}}}return list;}
}

转载于:https://www.cnblogs.com/shaer/p/10670108.html

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. 栈和递归的关系 144:Binary Tree Preorder Traversal

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

  5. Leetcode - 144. Binary Tree Preorder Traversal (层次遍历)

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

  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 ----- java

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

  8. 144. Binary Tree Preorder Traversal

    没什么要说的 1 public List<Integer> preorderTraversal(TreeNode root) { 2 List<Integer> res = n ...

  9. 144. Binary Tree Preorder Traversal 二叉树的前序遍历

    给定一个二叉树,返回它的 前序 遍历.  示例: 输入: [1,null,2,3] 1\2/3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? "> 给定一 ...

最新文章

  1. 一些重要的算法The Most Important Algorithms
  2. Python基础教程(九):面向对象、正则表达式
  3. 计算机中的进制和编码
  4. 分布式系统基本副本协议
  5. MATLAB数据分析3
  6. tableau三轴该怎么做_如何用tableau绘制城市地铁线路图?
  7. mysql中count(*)和count(1)和count(column)区别
  8. computed的原理
  9. 5010.有限状态机-电梯事件
  10. 大数据之-Hadoop3.x_MapReduce_ReduceJoin案例Reducer_案例完成---大数据之hadoop3.x工作笔记0131
  11. wpbakery Visual Composer - web网页可视化 编辑器 介紹
  12. 由MindManager命令构成的实用导图
  13. 一般处理程序里使用session对象为null,未将对象引用到实例化
  14. SVN客户端安装及操作文档
  15. Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据、人工智能、机器学习项目开发
  16. 视频、图形图像处理之Opencv技术记录(四)、OpenCV教程概述
  17. 写开源项目到底究竟有多赚钱?
  18. 信息泄漏时代,如何让自己的密码更安全?
  19. 使用WinGate代理服务器使局域网连接到Internet
  20. mariadb通用二进制格式安装

热门文章

  1. STM8控制4位LED数码管显示数字
  2. mybaits二十八:逆向工程
  3. vxworks中断的使用
  4. Zookeeper详解(一):分布式与Zookeeper
  5. 使用Action启动/关闭Service
  6. Shell脚本编程01:基础知识
  7. Flash cs5 初试
  8. ASP.NET3种验证码[转]
  9. 美国人的网站推广方式千奇百怪
  10. [转]奇文-闲话操作系统(1/4)