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 ...
随机推荐
- NPAPI命休矣
NPAPI命休矣,Firebreath命休矣,NPPluginProxy命休矣.以后该更多专注LLVM.Emscripten.Websocket和NativeClient之类的技术啦.
- Ideal-image-slider 幻灯片实例演示
链接:http://zaixianshouce.iteye.com/blog/2316300 http://www.shouce.ren/study/api/s/jq--5733e32bf23bb-- ...
- (转)java fail-fast机制
转自:http://blog.csdn.net/chenssy/article/details/38151189 Java提高篇(三四)-----fail-fast机制 标签: javajava提高篇 ...
- this,你是谁?
在js中this不像其它语言那样容易理解,它有时候指window对象,有时候又是其它对象,那么this,你到底是谁呢?要分析this就要先理解js中的方法定义,因为this一般都是在方法中使用的,而且 ...
- Node.js 入门 资源
Node.js 入门 <快速搭建 Node.js 开发环境以及加速 npm> http://fengmk2.com/blog/2014/03/node-env-and-faster-npm ...
- cocos2d安卓自动编译脚本去掉复制Resources资源和签名功能
去掉这两个功能的原因: 1.因为有时候打包是分渠道的,不同的渠道资源也可能不一样,所以不能直接复制资源. 2.如果用cocostudio打release包,因为要输入签名地址,会导致在自动签名处停住不 ...
- layoutSubviews何时调用的问题
本文转载至 http://www.cnblogs.com/pengyingh/articles/2417211.html 今天跟旺才兄学习了一下UIView的setNeedsDisplay和setNe ...
- Http服务器实现文件上传与下载(三)
一.引言 在前2章的内容基本上已经讲解了整个的大致流程.在设计Http服务器时,我设计为四层的结构,最底层是网络传输层,就是socket编程.接着一层是请求和响应层,叫做Request和Respons ...
- 多线程的设计模式--Future模式,Master-Worker模式,生产者-消费者模式
代码示例: public interface Data { String getRequest(); } public class FutureData implements Data{ privat ...
- [LintCode] 最后一个单词的长度
class Solution { public: /** * @param s A string * @return the length of last word */ int lengthOfLa ...