LeetCode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1\2/3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
代码如下:
class TreeNode
{int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }
}
class Solution
{public List<Integer> inorderTraversal(TreeNode root){Stack<TreeNode> stack = new Stack<TreeNode>();TreeNode current = root;List<Integer> ret = new ArrayList<Integer>();while (current != null || !stack.empty()){while (current != null){stack.push(current);current = current.left;}if (!stack.empty()){TreeNode tmp = stack.pop();ret.add(tmp.val);current = tmp.right;}}return ret;}
}
LeetCode Binary Tree Inorder Traversal相关推荐
- [leetcode]Binary Tree Inorder Traversal
二叉树的中序遍历非递归版本,采用的是wiki百科上的办法.果然简洁,而且和先序有异曲同工之妙,先序只用push右节点,中序只用push中节点.除此之外还有个更改TreeNode加一个visited的b ...
- Leetcode: Binary Tree Inorder Traversal
中序遍历 代码: #include <iostream> #include <vector> using namespace std;struct TreeNode {int ...
- 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal
LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...
- LeetCode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 15 二叉树的中序遍历(Binary Tree Inorder Traversal)
文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...
- [LeetCode]:94:Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- leetcode[94]Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [swift] LeetCode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
最新文章
- HDU1863(Prim算法)
- centos 6 上安装l7 filter尝试过滤xunlei
- 一个复杂系统的拆分改造实践
- arcgis栅格邻域统计_ArcGIS 从基础到实战书正式出版,易智瑞技术总裁沙志友沙总推荐并亲自写序...
- web自动化测试(java)---测试过程中遇到的错误合集
- HTML5 中的新特性:
- 清华又揽一位AI大牛!前微软全球副总裁开招博士生!
- Netty工作笔记0002---Netty的应用场景
- [导入]视频游戏开发系列课程(8):开始创造游戏的“精灵”的行为.zip(14.93 MB)
- Centos 查看CPU个数、核心数等信息
- 走进javascript——DOM事件
- 蒟蒻打CF#729div 2
- session的活化和钝化
- SEO分析关键词策略
- B站李沐讲论文笔记Transformer
- 真香,50行Java代码爬取妹子套图!
- burntest Linux参数,限拷机软件IntelBurnTest 2.0
- 建立里程碑,保证项目进度和质量
- 51单片机 | 蜂鸣器实验
- VSS2005局域网配置
热门文章
- 可执行文件启动器(下)
- 微软图表控件MsChart使用说明[转]
- C#与.NET程序员面试宝典 1.3.1 网络应聘(图)
- python 程序流程控制结构-【笔记】《python语言程序设计》——程序的控制结构...
- python简单代码画皮卡丘-实现童年宝可梦,教你用Python画一只属于自己的皮卡丘...
- 学python买什么书好-python官方推荐30本面向初学者的书籍!你看过几本?
- python使用方法-python-tkinter使用方法——转载(一)
- python怎么样才算入门编程-编程零基础应当如何开始学习 Python?
- 快速排序 python菜鸟教程-十大编程算法助程序员走上高手之路
- python输入语句-python输入语句