Leetcode: Binary Tree Inorder Transversal
- 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?
recursive 方法:
- /**
- * Definition for binary tree
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- ArrayList<Integer> res = new ArrayList<Integer>();
- if (root == null) return res;
- helper(root, res);
- return res;
- }
- public void helper(TreeNode root, ArrayList<Integer> res) {
- if (root == null) {
- return;
- }
- helper(root.left, res);
- res.add(root.val);
- helper(root.right, res);
- }
- }
Iterative method: 参考了一下网上的思路,其实就是用一个栈来模拟递归的过程。所以算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。
- /**
- * 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> inorderTraversal(TreeNode root) {
- List<Integer> res = new ArrayList<>();
- Stack<TreeNode> stack = new Stack<>();
- TreeNode p = root;
- while (p!=null || !stack.isEmpty()) {
- if (p != null) {
- stack.push(p);
- p = p.left;
- }
- else {
- TreeNode node = stack.pop();
- res.add(node.val);
- p = node.right;
- }
- }
- return res;
- }
- }
Leetcode: Binary Tree Inorder Transversal的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- Leetcode: Binary Tree Postorder Transversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
随机推荐
- Oracle 12C 创建用户连接pdb
测试环境: C:\ora12c\product\12.1.0\dbhome_1\BIN>sqlplus.exe /nolog SQL*Plus: Release 12.1.0.1.0 Produ ...
- mysql索引覆盖之innodb和myisam效率问题
问题: create table A ( id varchar(64) primary key, ver int, ... ) 我的表有几个很长的字段varchar(3000) 在i ...
- 用Android Studio导出jar给Unity3D用
1.新建一个Android Studio工程,选择空Activity 2.创建一个Module 3.将Unity的依赖jar包拷贝到工程的libs下 4.增加Java代码 内容修改如下 package ...
- [Windows] 使用SC 命令管理与配置服务
sc config wuauserv start= demand sc config wuauserv start= disabled
- android 关于view的onTouch和onClick同时触发解决方案
extends:http://blog.sina.com.cn/s/blog_aa0bd5950101gbwt.html 做了一个悬浮窗,需要处理onTouch和onClick事件, 1 定义一个bo ...
- Ubuntu 16.04系统下apt-get和dpkg区别
两者的区别是dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了,将会覆盖之前dpkg的安装.1.dpkg是用来安装.deb文件 ...
- Ubuntu16.04双网卡主备配置
前几日写了一篇Ubuntu14.04双网卡主备配置,没成想变化总是这么快,今日安装某软件,提示最匹配的ubuntu版本是16.04,作为一个码农能有什么办法,只能不断去适应变化.拥抱变化. 首先16. ...
- php下载文件,解压文件,读取并写入新文件
以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码: <?php //组织链接 $dataurl = "http://118.194.2 ...
- 基础回顾—list遍历4种
()for each for(bject o :list) { } [java] view plain copy ()Iterator Iterator iter = list.iterator(); ...
- ubuntu16.04配置tensorflow-gpu环境
1.安装驱动 参考: 史上最全的ubuntu16.04安装nvidia驱动+cuda9.0+cuDnn7.0 https://blog.csdn.net/qq_31215157/article/det ...