题目:二叉树的中序遍历。

思路:用递归来写中序遍历非常简单。但是题目直接挑衅说,----->"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的更多相关文章

  1. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  2. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  3. Leetcode Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  4. [Leetcode] Binary tree inorder traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. [LeetCode] Binary Tree Inorder Traversal 中序排序

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  6. leetcode第一刷_Binary Tree Inorder Traversal

    递归实现当然太简单,也用不着为了ac走这样的捷径吧..非递归实现还挺有意思的. 树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来.对于中序遍历,先要訪问最左下的节点,一定是进入循环后 ...

  7. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  9. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

随机推荐

  1. [ActionScript 3.0] AS3 GUID(全局唯一标识符)

    package com.controls { import flash.display.Sprite; import flash.system.Capabilities; public class G ...

  2. jquery ui datepicker中文显示

    $.datepicker.regional['zh-CN'] = { closeText: '关闭', prevText: '<上月', nextText: '下月>', currentT ...

  3. (整理).net实现条形码与二维码

    本文由来源网络的知识点组合而成,感谢分享的作者,文章结尾处给出查询资料连接. 条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.常见的条形码是 ...

  4. 项目积累——js应用

    //解决由前台向后台传值中文乱码的问题 encodeURI($("#xmjhbgFile").val())//前台JS中数据加码 String fjmc = java.net.UR ...

  5. (旧)子数涵数·PS——替换颜色

    一,老规矩,下载素材(老样子,还是美美的少女) 二.打开PS,并打开素材. 三.复制一个图层,快捷键Ctrl+J(好习惯) 四.使用"替换颜色"命令,弹出对话框. 五.选择人物衣服 ...

  6. Flex4 自定义通用的ImageButton

    Flex4与之前版本的一个极大区别就是外观皮肤的分离,虽然进一步解耦,但存在一个不爽的地方就是增加了编码的工作量,你能想象为你的每个自定义组件都写一个对应的皮肤吗?可能仅仅和你之前写过的组件差了那么一 ...

  7. Quick Sort(快排)

    这是挖坑填补法的演示 快排之挖坑填补法: void Quick(int top/*起始位置*/,int end/*末尾位置*/,int arr[])//挖坑填补法 { int i=top,j=end, ...

  8. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  9. NFS服务器

    NFS是网络文件系统(Network File System)的简称,可实现在多种网络上共享和装配远程文件系统.最大的功能是通过网络,让不同操作系统的计算机共享数据,所以它可以看成一个文件服务器. 1 ...

  10. 常用上传shell脚本

    #!/bin/bash FTP_IP=127.0.0.1 FTP_USER="aaa" FTP_PSW="bbb" GAME_NAME="ccc&qu ...