[LeetCode] 144. Binary Tree Preorder Traversal Java
题目:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1\2/3
return [1,2,3]
.
题意及分析:给出一个二叉树,求先序遍历的结果。使用递归很简单,但是要求不用递归,所以我们可以使用一个stack来实现非递归方法,维护一个stack,保存从根节点一直到最左边的叶节点上的点,同时将这些点的值添加进结果集,达到叶节点后需要查看该节点是否有兄弟节点(即该点的父节点是否有右子节点),若有则对该兄弟节点同样遍历到最左叶节点,然后返回上一层,以此类推。具体实现看代码:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> stack=new Stack<>();while(root!=null||!stack.isEmpty()){if(root!=null){stack.push(root);res.add(root.val);root = root.left;}else{root=stack.pop();root=root.right;}}return res;}
}
转载于:https://www.cnblogs.com/271934Liao/p/7017763.html
[LeetCode] 144. Binary Tree Preorder Traversal Java相关推荐
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- 【二叉树迭代版前序遍历】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 ...
- 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++,Python解法
题目地址:Binary Tree Preorder Traversal - LeetCode Given a binary tree, return the preorder traversal of ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a binary ...
- 144. Binary Tree Preorder Traversal(非递归实现二叉树的前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- 栈和递归的关系 144:Binary Tree Preorder Traversal
前序遍历:根左右 //用栈来实现非递归解法/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode ...
最新文章
- class struct
- 接口经常超时?线程池+ FutureTask来解决!
- java writestartarray_jackson的基础知识
- fft函数图像横坐标是什么_10分钟学会:函数图像的平移与伸缩变换
- Kafka—简明教程
- SpringSecurity分布式整合之认证服务配置文件编写和测试
- 【双百解法】剑指 Offer 11. 旋转数组的最小数字
- 有一个懂机械、懂焊接、懂电子的爹是什么概念。。。
- sql limit不接具体数字_这21个写SQL的好习惯,你要养成呀
- java和C++有什么异同
- “全息数字人”——健康医疗 大数据应用的新模式
- html第一章咖啡作业,CSS基础选择器温故-1 - 怪诞咖啡
- Openstack概论
- 问题:子进程父进程哪个先执行:【转】关于 fork 和父子进程的理解
- 循环buffer的实现_Go语言源码阅读之bytes.Buffer
- 绘制ER图:PowerDesigner数据库设计软件讲解
- 【随便说说】|| 老师用腾讯会议开直播,实在忍不住了,我......
- Mybatis新增获取Id
- 2019牛客暑期多校训练营(第八场)E.Explorer
- 细化(thinning)
热门文章
- MVC源码解析 - 配置注册 / 动态注册 HttpModule
- PHP 中获取文件名及路径
- 1074: [SCOI2007]折纸origami - BZOJ
- 闭包的理解、缺点以及应用场景
- Linux---网络编程基础
- Vue报错:3 errors and 0 warnings potentially fixable with the `--fix` option.
- 如何看出男朋友有没有潜力?
- 感觉越来越多的人开始向往农村生活,你怎么看?
- 早晨有好多学生在买早餐吃,这样好吗?家长不给孩子做饭吗?
- 10元一瓶的普通白酒和2000元一瓶的茅台酒有什么区别?