package cn.edu.xidian.sselab.hashtable;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author zhiyong wang
 * title: Binary Tree Inorder Traversal
 * content:
 * 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]
 *
 */
public class BinaryTreeInorderTraversal {

//with stack to solve this question
    public List<Integer> inorderTraversal(TreeNode root){
        TreeNode node = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        List<Integer> list = new ArrayList<Integer>();
        while(node != null || !stack.isEmpty()){
            while(node != null){
                stack.push(node);
                node = node.left;
            }
            if(!stack.isEmpty()){
                node = stack.pop();
                list.add(node.val);
                node = node.right;
            }
        }
        return list;
    }
    
    //with recursive to solve this question
    //这个地方注意三个地方:(1)if(node == null)的时候,返回的是list,而不是null
    //                   (2) 题目中定义的是left与right,而不是leftNode,rightNode
    //                    (3)递归返回的结果要加到list中,所以用list.addAll();
    public List<Integer> inorderTraversals(TreeNode root){
        TreeNode node = root;
        List<Integer> list = new ArrayList<Integer>();
        if(node == null) return list;
        list.addAll(inorderTraversals(node.left));
        list.add(node.val);
        list.addAll(inorderTraversals(node.right)) ;
        return list;
    }
    
}

class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val){
        this.val = val;
    }
}

Binary Tree Inorder Traversa的更多相关文章

  1. LintCode Binary Tree Inorder Traversal

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

  2. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  4. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

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

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

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

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

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

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

  8. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

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

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 玩转OpenStack网络Neutron(2)--使用Open vSwitch实现VLAN类型租户网络

    欢迎转载,转载请保留原作者信息 欢迎交流学习,共同进步! 作者:颜海峰 个人博客:http://yanheven.github.io 微博:海峰_云计算 http://weibo.com/344736 ...

  2. 定制属于自己的Chrome起始页

    个人感觉没什么技术含量,可是很有用.我定制的起始页面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ19saWNodW4=/font/5 ...

  3. is not in the sudoers file.This incident will be reported

    解决方法如下: 1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式. 2>.添加文件的写权限.也就是输入命令&q ...

  4. linux之CentOS-7.0环境搭建

    此文作为新手安装centos-7的图文教程.  一.  前言 最近,师兄要进行实验室架构搭建,需要学习docker.而docker是完全依赖于linux系统的.所以,有了这篇文章. linux有很多发 ...

  5. jQuery中在当前页面弹出一个新的界面

    W.$.dialog({ content:'url:wswgrkbillController.do?snh&id='+b+'&bh='+c+'&ck='+d+'&sl= ...

  6. 统计机器翻译(SMT)步骤总结

    本文是在Niutrans论坛中的系列教程中总结出来的. 1.语料预处理 预处理的结果是生成双语分词之后的文件,该步需要注意的是对规则短语,比如数字.日期.网址等,进行泛化处理.可以用正则方法或者其它方 ...

  7. Swift - 26 - 函数的基础写法

    //: Playground - noun: a place where people can play import UIKit // 无参无返回 // -> Void可以省略不写, 或者写成 ...

  8. C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体

    //浏览图片 private void btnUp_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialo ...

  9. [转]Delphi : keydown与keypress的区别,组合键

    Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...

  10. java 懒汉式--初步解决安全问题

    2016-07-28 00:10:14 懒汉式: class text { public String k;       private static text t=null;//右边代码结构比上边饿 ...