import java.util.Arrays;
import java.util.Stack;
import java.util.TreeMap; /**
*
* Source : https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
*
*
* 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?
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*
*/
public class BinaryTreeInOrderTraversal {
private int[] result = null;
int pos = 0; public int[] traversal(char[] tree) {
result = new int[tree.length];
pos = 0;
traversalByRecursion(createTree(tree));
return result;
} public int[] traversal1(char[] tree) {
result = new int[tree.length];
pos = 0;
traversalbyIterator(createTree(tree));
return result;
} /**
* 对二叉树进行中序遍历
*
* 树的遍历分为:
* 深度优先:
* 先序遍历:先访问根节点然后依次访问左右子的节点
* 中序遍历:先访问左子节点,然后访问根节点,在访问右子节点
* 后序遍历:先访问左右子节点,然后访问根节点
*
* 先用递归实现中序遍历
*
* @param root
* @return
*/
public void traversalByRecursion(TreeNode root) {
if (root == null) {
return;
}
traversalByRecursion(root.leftChild);
result[pos++] = root.value;
traversalByRecursion(root.rightChild);
} /**
* 使用循环来进行中序遍历,借助栈实现
*
* @param root
*/
public void traversalbyIterator (TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while (cur != null || stack.size() > 0) {
if (cur == null) {
// 当前节点为空,表示已经是叶子节点,
TreeNode node = stack.pop();
result[pos++] = node.value;
cur = node.rightChild;
} else {
stack.push(cur);
cur = cur.leftChild;
}
}
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() {
}
} public static void main(String[] args) {
BinaryTreeInOrderTraversal binaryTreeInOrderTraversal = new BinaryTreeInOrderTraversal();
char[] arr1 = new char[]{'1','#','2','3'};
char[] arr2 = new char[]{'1','2','3','#','#','4','#','#','5'};
System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal(arr1)));
System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal(arr2))); System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal1(arr1)));
System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal1(arr2)));
}
}

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 python

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

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

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

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

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

  9. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  10. 49. leetcode 94. Binary Tree Inorder Traversal

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

随机推荐

  1. 1.初识Node.js

    Node.js基础知识大汇总 1.下载并安装npm,检测安装是否成功(在命令行输入node -v,看是否会输出对应版本号) 2.写一个hello world 程序. (1).打开notepad,新建一 ...

  2. Pi 3B+编译安装python3.6.8

    树莓派镜像版本2018-11-13,更新到2019-01-09 sudo apt-get update sudo apt-get upgrade -dev libgdbm-dev libsqlite3 ...

  3. docker遇到超时

    1. 直接通过docker拉取镜像遇到的问题:熟悉的timeout!!! [root@localhost ~]# docker search mysql INDEX NAME DESCRIPTION ...

  4. C#中添加log4net(日志文件)

    1.先下载引用“log4net” 2.然后再App.config配置 3.添加一个LogHandler类 4.在Assemblyinfo类中添加配置的读取文件 5.运用日志文件 6.显示结果

  5. mysql爱之深探测

    第一:函数 一:内置函数 MYSQL中提供了很多内置的函数,以下: CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二 ...

  6. mui项目实时更新

    var wgtVer=null; function plusReady(){ // ...... // 获取本地应用资源版本号 plus.runtime.getProperty(plus.runtim ...

  7. 255.Spring Boot+Spring Security:使用md5加密

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  8. JNI实战(一):JNI HelloWorld

    使用最新Android Studio的Cmake,创建一个Native C++项目后,我们就可以看到JNI的Hello World的项目及示例代码了. JNI的项目代码,分为三层:Java层,C++层 ...

  9. [Swift]LeetCode26. 删除排序数组中的重复项 | Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  10. [Swift]LeetCode124. 二叉树中的最大路径和 | Binary Tree Maximum Path Sum

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...