Binary Tree Inorder Traversal leetcode java
题目:
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?
题解:
中序遍历:递归左 处理当前 递归右。
画图的话就是,之前离散老师教的,从root开始沿着子树画线,遍历完全树,每一个被画线画到2次的就表示遍历到了,依次写出就行了。
递归代码如下:
1 public void helper(TreeNode root, ArrayList<Integer> re){
2 if(root==null)
3 return;
4 helper(root.left,re);
5 re.add(root.val);
6 helper(root.right,re);
7 }
8 public ArrayList<Integer> inorderTraversal(TreeNode root) {
9 ArrayList<Integer> re = new ArrayList<Integer>();
if(root==null)
return re;
helper(root,re);
return re;
}
非递归代码如下:
1 public ArrayList<Integer> inorderTraversal(TreeNode root) {
2 ArrayList<Integer> res = new ArrayList<Integer>();
3 if(root == null)
4 return res;
5 LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
6 while(root!=null || !stack.isEmpty()){
7 if(root!=null){
8 stack.push(root);
9 root = root.left;
}else{
root = stack.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
Binary Tree Inorder Traversal leetcode java的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Binary Tree Inorder Traversal -- LeetCode 94
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Inorder Traversal ——LeetCode
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Preorder Traversal leetcode java
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...
- [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)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
随机推荐
- 在控制台连接oracle
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\lijt>sqlplus ...
- SpringBoot常用配置
前言:springboot集成了主流的第三方框架,但是需要使用springboot那一套配置方式.但是我这里只列举了非常非常常用的,可以看已发的几篇博客,慢慢会补充.当然官方文档里也有相应的配置,可惜 ...
- 在centOS使用systemctl配置启动多个tomcat
公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...
- 美团外卖iOS多端复用的推动、支撑与思考
背景 美团外卖2013年11月开始起步,随后高速发展,不断刷新多项行业记录.截止至2018年5月19日,日订单量峰值已超过2000万,是全球规模最大的外卖平台.业务的快速发展对技术支撑提出了更高的要求 ...
- ICMP隧道工具ptunnel
ICMP隧道工具ptunnel 在一些网络环境中,如果不经过认证,TCP和UDP数据包都会被拦截.如果用户可以ping通远程计算机,就可以尝试建立ICMP隧道,将TCP数据通过该隧道发送,实现不受 ...
- Vue-router浅识
一.router-link及router-view :用来做导航,通过传入to属性来指定链接 :用来做路由出口,路由匹配到的组件都会渲染在这里 const router = new VueRouter ...
- 安卓中WebKit的使用
1.在安卓开发中,使用webkit显示网页 步骤: ①初始化一个webkit控件: ②获取webkit的WebSettings对象: ③设置javascript为enable ④为webkit设置&q ...
- centos7下redis-2.8.13安装笔记
安装gcc 执行命令:yum install gcc 中间有两次提示,要求输入:yes 或 y,我们按照提示来就可以 至此安装成功 注意:如果不装gcc,解压redis后执行make命令会报错,如下图 ...
- 通过maven-war-plugin插件对war包分环境打包
针对多环节,从源头打包入手,当然这些都可以在运维阶段用脚本进行替换来代替 resources/environment/下有四个环境,local本地.dev开发.test测试.pre预上线.prod生产 ...
- java8函数式接口小例子
// Function<T, R> -T作为输入,返回的R作为输出 Function<String,String> function = (x) -> {System.o ...