Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
/**
* 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> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
Java实现 LeetCode 94 二叉树的中序遍历的更多相关文章
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode 94. 二叉树的中序遍历】解题报告
前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> inorderTraversal(TreeNode* root ...
- LeetCode 94 ——二叉树的中序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 将当前节 ...
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
随机推荐
- LeetCode二分专题
二分 二分模板 两个模板:1.最大值最小模板一,2.最小值最大用模板二 单调性.两段性的性质 版本1:二分绿色端点是答案,最大值最小 int bsearch_1(int l, int r){ whil ...
- [codeforces-315D div2]模拟
题目:给两个字符串a.b,问从a中删去若干字符后最多可以得到多少个b串的重复串(bb...b的形式,b的长度不超过100),其中a串是由一个长度不超过100的字符串s重复k次得到的 思路: 暴力匹配a ...
- ThinkPad BIOS
ThinkPad-BIOS 如有问题指出 ~谢谢
- Redis系列(七)Redis面试题
Redis 系列: Redis系列(一)Redis入门 Redis系列(二)Redis的8种数据类型 Redis系列(三)Redis的事务和Spring Boot整合 Redis系列(四)Redis配 ...
- es7,8 临门一脚。
ES7 1.Array.prototype.includes() includes()作用,是查找一个值在不在数组里,若是存在则返回true,不存在返回false. 1.基本用法: ['a', 'b' ...
- Android平台使用termux,随时随地写代码
生活如此无聊,写点代码打发时间. 趁着假期,我的vivo手机和华为平板(均为4G+64G配置)用的也比较少.于是,思考着如何将这2个设备用来写latex.python.用kingroot却取不了两设备 ...
- ubuntu软件管理工具的使用——dpkg和apt
deb.rpm.tar.gz三种Linux软件包的区别在哪里呢,这种区别可能使安装进行不下去,那么我们应该下载什么格式的包呢?下面具体讲解一下. rpm包是在Redhat.Suse和Fedora可以直 ...
- Python--WebDriverWait+expected_conditions的一个应用
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...
- flask之Flask、config配置
flask_config.py ''' flask的配置: 1.flask项目初始化配置: (1)app=Flask(__name__)#初始化声明falsk项目为当前py文件,app应用变量名可以更 ...
- python3.x 基础一
1.python 格式化print #/usr/bin/env python #-*- coding: utf-8 -*- #Author:jenvid.yang # 连接符号+,可以连接多个变量或者 ...