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. BZOJ.4842.[NEERC2016]Delight for a Cat(费用流)

    BZOJ 参考这儿. 首先如果一个活动的时间满足条件,那么另一个活动也一定满足.还有就是这题就是费用流没有为什么.不妨假设最初所有时间都用来睡觉,那么我们要对每个\(k\)大小区间选出\([t2,k- ...

  2. Exp5 MSF基础应用 20164302 王一帆

    1.实验内容 1.1实验要求 一个主动攻击实践,ms08_067; 一个针对浏览器的攻击,MS10-018(成功且唯一),MS12-004(成功且唯一): 一个针对客户端的攻击,adobe_toolb ...

  3. NOIP2006普及组 Jam的计数法

    普及组重要的模拟题.附上题目链接 https://www.luogu.org/problem/show?pid=1061 (写水题题解算是巩固提醒自己细心吧qwq) 样例输入: bdfij 样例输出: ...

  4. js计算发表的时间...分钟/小时以前/以后

    网上找的都好复杂,这本来就是个粗略显示通俗的时间,绕来绕去都晕了 function timeAgo(o){ var n=new Date().getTime(); var f=n-o; var bs= ...

  5. Android四大组件的简介

    Android开发四大组件分别是: 一.活动(Activity): 用于表现功能.二.服务(Service): 后台运行服务,不提供界面呈现. 三.广播接收器(BroadcastReceiver):用 ...

  6. Javaweb-request与response

                           Javaweb-request与response              Servlet   生命周期(什么时候创建的,什么时候销毁的);     Se ...

  7. 使用datagrip链接mysql数据库的报错问题.

    1. datagrip刚打开时候,选择风格是白是黑后, 会有一个选择什么数据库,有oracle...一大堆,别选错了.我的是mysql,不要选成了windows sql 和sql. 2 基本设置写完, ...

  8. bootstrap table 标题列重复

    使用bootstrap table生成表格,出现一个奇怪问题,标题列重复.查了一大堆资料,没有找到可以解决问题的. 以为是类库版本的问题,全部替换成了example中的引用,还是这个问题. 后来仔细查 ...

  9. Three.js学习笔记05

    场景相关函数和属性 下面的代码中应用到了所有以上的函数及属性: <!DOCTYPE html> <html lang="en"> <head> ...

  10. PostgreSQL+PostGIS 的使用

    一.PostGIS中的几何类型 PostGIS支持所有OGC规范的“Simple Features”类型,同时在此基础上扩展了对3DZ.3DM.4D坐标的支持. 1. OGC的WKB和WKT格式 OG ...