leetcode — binary-tree-inorder-traversal
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的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
随机推荐
- Centos7安装zabbix-agent
1.下载zabbix-agent wget https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.10 ...
- centos7系统下 docker 环境搭建
运行环境: VMware Workstation Pro 在虚拟机中安装centos7系统, 选择最小安装, 网络连接方式选择的桥接(与宿主机在同一IP段)centos7一定要安装64位, docke ...
- 不定高元素的高度transition动画实现
分析文档描述 CSS 支持动画的属性中的 height 属性如下: height :yes, as a length, percentage or calc() 即:当 height 的值是 leng ...
- 如何查看ubuntu系统版本信息
第一种方法: hadoop@master:~$ cat /proc/version Linux version 4.4.0-21-generic (buildd@lgw01-21):Linux内核版本 ...
- MySQL8主从配置
最近在看MySQL的主从配置,罗列一下过程. 一.环境介绍 我使用的是两个MySQL8.0.13Windows版,Master和Slave安装的在一个机器上,Master库的端口为3306,Slave ...
- React组件和生命周期简介
React 简介----React 是 Facebook 出品的一套颠覆式的前端开发类库.为什么说它是颠覆式的呢? 内存维护虚拟 DOM 对于传统的 DOM 维护,我们的步骤可能是:1.初始化 ...
- [LeetCode] Keys and Rooms 钥匙与房间
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, an ...
- 详解微信小程序开发(项目从零开始)
一.序 微信小程序,估计大家都不陌生,现在应用场景特别多.今天就系统的介绍一下小程序开发.注意,这里只从项目代码上做解析,不涉及小程序如何申请.打包.发布的东西.(这些跟着微信官方文档的流程走就好). ...
- jenkins配置演示
构建代码的几个名词: make:linux或者windows最原始的编译工具,在Linux下编译程序常用make,windows下对应的工具为nmake.它负责组织构建的过程,负责指挥编译器如何编译, ...
- input type=passoord 密码框的明密文(显示和隐藏) 显示
最近在写一个新的项目,从头开始写,所以就要从注册登录开始做起.以前写登录注册模块的时候,无外乎给input框一个type=”password”就可以了,近期因为要涉及到显示隐藏状态的切换. 样式代码如 ...