[leetcode]_Binary Tree Inorder Traversal
题目:二叉树的中序遍历。
思路:用递归来写中序遍历非常简单。但是题目直接挑衅说,----->"Recursive solution is trivial"。好吧。谁怕谁小狗。
递归代码:
List<Integer> inOrder = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
inOrderT(root);
return inOrder;
}
public void inOrderT(TreeNode root){
if(root != null){
inorderTraversal(root.left);
inOrder.add(root.val);
inorderTraversal(root.right);
}
}
循环解法:利用stack,由于中序遍历在访问完节点的左子树后访问节点值,因此,当前node != null时,将node 入栈,访问其左子树;当左子树为null了,从栈中弹出元素访问;再访问其右子树。
------>写的非常好的二叉树遍历的文章
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> inOrder = new ArrayList<Integer>();
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode help = root;
while(help != null || !s.isEmpty()){
while(help != null){
s.push(help);
help = help.left;
}
if(!s.isEmpty()){
help = s.pop();
inOrder.add(help.val);
help = help.right;
}
}
return inOrder;
}
[leetcode]_Binary Tree Inorder Traversal的更多相关文章
- 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
递归实现当然太简单,也用不着为了ac走这样的捷径吧..非递归实现还挺有意思的. 树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来.对于中序遍历,先要訪问最左下的节点,一定是进入循环后 ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- [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
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
随机推荐
- 20145305 《Java程序设计》实验一
实验名称 实现凯撒密码,并进行测试. 实验内容 它是一种代换密码.据说凯撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码. 凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经 ...
- ext 3.2 tree 在IE10中点击事件失效的bug
ext3.2 中的tree在IE中进行兼容性测试,遇到IE10时,无法点击,其他版本的IE(7.8.9.11)均正常.此bug是由于ext-all.js中的getAttributeNS方法不能兼容IE ...
- [SQL]SQL删除数据的各种方式总结
SQL删除数据的各种方式总结 一.使用DELETE从表中删除目标行.记录每次删除操作.如: USE pubs DELETE FROM authors WHERE au_lname = 'McBadde ...
- oracle行列转换函数的使用
oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...
- Spark-1.5.2安装
1.下载scala-2.10.6包解压到指定目录 #SCALA VARIABLES START export SCALA_HOME=/usr/local/scala-2.10.6 export PAT ...
- oracle rac 日志体系结构!
告警日志集群节点集群件告警日志:$GRID_HOME/log/<hostname>/alert<hostname>.log数据库实例的告警日志:$DIAG_DESTINATIO ...
- FileZilla - Windows XP经典软件系列
官网: https://filezilla-project.org/ 下载: http://sourceforge.net/projects/filezilla/ 版本:V3.9.0.1 (支持XP最 ...
- Prim算法POJ1258
http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, #include<stdio.h> #include<iostream> #inc ...
- 普通session vs MemcachedSession vs RedisSession
一.普通session(数据存储在内存中) #!/usr/bin/env python # -*- coding:utf-8 -*- from hashlib import sha1 import o ...
- 同步灵无线锂电鼠G11-580HX独特“五灵键”
http://tieba.baidu.com/p/2130455709 现在鼠标功能越来越强大,游戏鼠标的宏设置,办公鼠标的复制粘贴等.各位有没有给鼠标设置一些好玩.方便.搞怪.有意思的的功能的呢?笔 ...