94. Binary Tree Inorder Traversal(二叉树中序遍历)
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1
\
2
/
3
return [1,3,2]
.
递归:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
help(root);
return res;
}
private void help(TreeNode root){
if(root==null) return ;
if(root.left!=null)
help(root.left); res.add(root.val); if(root.right!=null)
help(root.right);
}
}
非递归:
终极版:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
while(root!=null||!s.isEmpty()){
while(root!=null){
s.push(root);
root=root.left;
}
if(!s.isEmpty()){
root = s.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
}
利用辅助桟
class Solution { public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while(cur!=null ||!stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur =cur.left;
}
cur = stack.pop();
res.add(cur.val);
cur =cur.right;
}
return res;
} }
94. Binary Tree Inorder Traversal(二叉树中序遍历)的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历
题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- [Win10应用开发] 使用 Windows 推送服务
前言 Windows 推送服务(WNS)也是 Win10 通知机制中的一种,今天与大家一起学习一下有关WNS的相关知识.使用 Windows 推送服务的前提是你需要有一个微软开发者账号,这样才能得到一 ...
- Java 设计模式01 - 简单工厂模式
先要学习设计模式之前的先看看一些基础 UML类图简单说明 可以先看看我的这篇博客: UML类图简单说明,学习编程思路的必会技能 接下来才是重点,开始我们的旅程吧. 一.UML类图展示 我们要用简单工厂 ...
- python笔记5:装饰器、内置函数、json
装饰器 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 先看简单例子: def run(): time.sleep(1 ...
- 使用scp命令传输文件
1. 从远端复制文件到本地: sudo scp root@192.168.0.1:remote_path/remote_file . 2. 从本地复制文件到远端: sudo scp local_fil ...
- 【vijos】1729 Knights(匈牙利)
https://vijos.org/p/1729 这题好奇葩,为嘛N开到30就会re啊..........n<=26吗.... sad 因为根据棋子的分布,能攻击的一定各在一黑白格上,所以直接二 ...
- FreeRTOS系列第17篇---FreeRTOS队列
本文介绍队列的基本知识,具体源代码分析见<FreeRTOS高级篇5---FreeRTOS队列分析> 1.FreeRTOS队列 队列是基本的任务间通讯方式.能够在任务与任务间.中断和任务间传 ...
- 浅析Java与C#的事件处理机制
http://www.cnblogs.com/OOAbooke/archive/2012/02/18/2356899.html
- iOS -转载-字符串是否为空判断方法
- (BOOL)blankString{ if (![self isKindOfClass:[NSString class]] ){ return YES; } if ([self isEqual:[ ...
- Laravel5.1 模型 --多态关联
什么是多态关联? 一个例子你就明白了:好比如说评论 它可以属于视频类 也可以属于文章类,当有个需求是 从评论表中取到视频类的数据,这就需要用到多态关联了. 简单的一句话总结:一张表对应两张表. 1 实 ...
- oracle游标:查询并打印员工的姓名和薪水
--查询并打印员工的姓名和薪水 --set serveroutput on /* 1.光标的属性 %found:假设取到了记录就是true否则是false: %notfound: */ declare ...