import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
*
* Source : https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*
*
* Given preorder and inorder traversal of a tree, construct the binary tree.
*
* Note:
* You may assume that duplicates do not exist in the tree.
*/
public class ConstructFromPreorderAndInorder { /**
* 使用前序和中序遍历结果来恢复一颗二叉树
* preorder:root/left/right
* inorder:left/root/right
*
* 根据preorder找到root,然后根据inorder找到left,right
*
* @param preorderArr
* @param inorderArr
* @return
*/
public TreeNode build (char[] preorderArr, char[] inorderArr) {
return buildByRecursion(preorderArr, 0, preorderArr.length-1, inorderArr, 0, inorderArr.length-1);
} public TreeNode buildByRecursion (char[] preorerArr, int preStart, int preEnd, char[] inorderArr, int inStart, int inEnd) {
if(preStart > preEnd || inStart > inEnd) {
return null;
} TreeNode root = new TreeNode(preorerArr[preStart] - '0');
int rootIndex = -1;
for (int i = inStart; i <= inEnd; i++) {
if (preorerArr[preStart] == inorderArr[i]) {
rootIndex = i;
break;
}
} if (rootIndex < 0) {
return null;
}
int leftTreeSize = rootIndex - inStart;
int rightTreeSize = inEnd - rootIndex;
root.leftChild = buildByRecursion(preorerArr, preStart+1, preStart + leftTreeSize,
inorderArr, inStart, rootIndex-1);
root.rightChild = buildByRecursion(preorerArr,preEnd-rightTreeSize+1, preEnd,
inorderArr, rootIndex+1, inEnd );
return root; } /**
* 使用广度优先遍历将数转化为数组
*
* @param root
* @param chs
*/
public void binarySearchTreeToArray (TreeNode root, List<Character> chs) {
if (root == null) {
chs.add('#');
return;
}
List<TreeNode> list = new ArrayList<TreeNode>();
int head = 0;
int tail = 0;
list.add(root);
chs.add((char) (root.value + '0'));
tail ++;
TreeNode temp = null; while (head < tail) {
temp = list.get(head);
if (temp.leftChild != null) {
list.add(temp.leftChild);
chs.add((char) (temp.leftChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
if (temp.rightChild != null) {
list.add(temp.rightChild);
chs.add((char)(temp.rightChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
head ++;
}
//去除最后不必要的
for (int i = chs.size()-1; i > 0; i--) {
if (chs.get(i) != '#') {
break;
}
chs.remove(i);
}
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
/*
* 3
* / \
* 9 2
* / \
* 1 7
*/ char[] preorderArr = new char[]{'3','9','2','1','7'};
char[] inorderArr = new char[] {'9','3','1','2','7'}; ConstructFromPreorderAndInorder constructFromPreorderAndInorder = new ConstructFromPreorderAndInorder(); TreeNode root = constructFromPreorderAndInorder.build(preorderArr, inorderArr);
List<Character> chs = new ArrayList<Character>();
constructFromPreorderAndInorder.binarySearchTreeToArray(root, chs);
System.out.println(Arrays.toString(chs.toArray(new Character[chs.size()]))); } }

leetcode — construct-binary-tree-from-preorder-and-inorder-traversal的更多相关文章

  1. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. Leetcode Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

    Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...

  5. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...

  6. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

  7. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  8. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  10. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

随机推荐

  1. tensorflow 使用 2 Felch ,Feed

    Felch ::在会话里可以执行多个 op , import tensorflow as tf input1 = tf.constant(3.0) input2 = tf.constant(2.0) ...

  2. 刚学的vue.js的单一事件管理组件通信

    第一次在博客园写的技术分享,写的不好的话各位大神多体谅,好啦进入主题 说说思路 首先 第一步,准备一个空的示例对象 var Event=new Vue(); 第二步,准备发送的数据 Event.$em ...

  3. acl权限命令

    1.查看acl命令 getfacl 文件名 #查看acl权限 2.设定acl权限命令 setfacl 选项 文件名 选项: -m 设置ACL权限 -x 删除指定的ACL权限 -b 删除所有的ACL设定 ...

  4. SpringBoot几种定时任务的实现方式

    定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...

  5. ionic4 开发企业微信应用0

    作为一个后台开发人员,几年前参与过Ionic1开发过一微信公众号的经历,所以这次开发企业微信应用,就使用了ionic,正好ionic4 rc版本发布,虽然不是正式版,作为本项目的项目经理,还是决定使用 ...

  6. [LeetCode] Hand of Straights 一手顺子牌

    Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...

  7. 微信小程序开发工具中快捷键

    微信小程序开发工具表面上是没有更多的样式类的工具,例如缩进.隐藏代码什么的. 现在总结一下小程序开发工具常用的一些快捷键: 格式调整 Ctrl+S:保存文件Ctrl+[, Ctrl+]:代码行缩进Ct ...

  8. lua语言自学知识点----简单了解

    零碎知识点: lua:用lua写UI,更新UI,因为lua可直接跨平台解析,不需要编译,方便更新------>热更新. c#反射也可以达到更新,但非常麻烦,切不支持iOS. 在lua中一个人汉字 ...

  9. 关于使用jquery对input中type为radio的标签checked属性的增加与移除

    需求:对radio的checked属性先消除然后进行重新设置: 初步方案: $("auForm input :radio[value='0']").removeAttr('chec ...

  10. SimpleRpc-客户端与服务端工作模型探讨

    前言 本篇文章讲述客户端与服务端的具体设计细节.有细心的小伙伴发现,客户端和服务端的工作方式不一样:服务端是多线程计算模型,利用工作线程完成数据的读取,而客户端是单线程(利用Reactor线程完成数据 ...