144. Binary Tree Preorder Traversal(非递归实现二叉树的前序遍历)
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(非递归实现二叉树的前序遍历)相关推荐
- 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal
LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...
- [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 ...
- leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- 栈和递归的关系 144:Binary Tree Preorder Traversal
前序遍历:根左右 //用栈来实现非递归解法/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode ...
- 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 Java
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- 144. Binary Tree Preorder Traversal
没什么要说的 1 public List<Integer> preorderTraversal(TreeNode root) { 2 List<Integer> res = n ...
- 144. Binary Tree Preorder Traversal 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1\2/3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? "> 给定一 ...
最新文章
- 一些重要的算法The Most Important Algorithms
- Python基础教程(九):面向对象、正则表达式
- 计算机中的进制和编码
- 分布式系统基本副本协议
- MATLAB数据分析3
- tableau三轴该怎么做_如何用tableau绘制城市地铁线路图?
- mysql中count(*)和count(1)和count(column)区别
- computed的原理
- 5010.有限状态机-电梯事件
- 大数据之-Hadoop3.x_MapReduce_ReduceJoin案例Reducer_案例完成---大数据之hadoop3.x工作笔记0131
- wpbakery Visual Composer - web网页可视化 编辑器 介紹
- 由MindManager命令构成的实用导图
- 一般处理程序里使用session对象为null,未将对象引用到实例化
- SVN客户端安装及操作文档
- Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据、人工智能、机器学习项目开发
- 视频、图形图像处理之Opencv技术记录(四)、OpenCV教程概述
- 写开源项目到底究竟有多赚钱?
- 信息泄漏时代,如何让自己的密码更安全?
- 使用WinGate代理服务器使局域网连接到Internet
- mariadb通用二进制格式安装