剑指 Offer 07. 重建二叉树
1.结果
执行结果:通过显示详情〉
执行用时:8 ms ,在所有Java提交中击败了13.35%的用户
内存消耗:72.9 MB ,在所有Java提交中击败了8.90%的用户
通i过测]试用例:203/ 203
只是通过了,效果不佳,待进一步学习
2.时间花费
花费了一整个晚上,大约6个小时,主要原因是审题不清楚,多余实现了树的先序排列(用于检查自己构建的二叉树是否正确),以及实现了二叉树的层级遍历,以及做了最后因为包含填充null的原因,所以还有去除null的操作。
3.思路
3.1 前提概念:
二叉树的三种顺序遍历:指三节点中的中间根节点在相邻两个节点中的位置
- 前序遍历
- 中序遍历
- 后序遍历
3.2 方法一
这个是自己实现的。
使用递归就可以了。
就是按照手算的顺序,一步一步根据根节点划分左右子树,并递归,递归的时候根据根节点索引位置判断是否有左右子树
3.2 方法二
迭代法。略。
4.code
/**
* 剑指 Offer 07. 重建二叉树
* <p>
* 输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
* 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
/**
*
* 第一步:复原二叉树【只要完成这一步就可以了,后面的是多余的】
* 前序第一个一定是当前根节点
* 不含重复数字,则中序和根节点将其划分为左右子树
* 当一直分下去,直到左右子树不再有子节点就不用再分了
*
* 第二步:层遍历二叉树
* 每次访问根节点,广度搜索,使用队列结构
* 按层访问时,空的用null填充{不知道怎么填充null}
*
* 第三步:去除第二步末尾收集到的多余的null
*/
//第一步:得到二叉树
TreeNode tn = getTree(preorder, inorder);
printTreePreOrder(tn);
System.out.println("build over!");
//第二步:层遍历
ArrayList<Integer> re = new ArrayList<>();
LinkedList<TreeNode> queue = new LinkedList<>();
LinkedList<Integer> queueVal = new LinkedList<>();
queue.add(tn);
queueVal.add(tn.val);
while (queue.size() != 0) {
System.out.println(Arrays.toString(queueVal.toArray()));
tn = queue.remove();
queueVal.remove();
if (tn!=null){
re.add(tn.val);
if (tn.left != null) {
queue.add(tn.left);
queueVal.add(tn.left.val);
} else {
queue.add(null);
queueVal.add(null);
}
if (tn.right != null) {
queue.add(tn.right);
queueVal.add(tn.right.val);
} else {
queue.add(null);
queueVal.add(null);
}
}else {
re.add(null);
}
}
System.out.println(Arrays.toString(re.toArray()));
//去除最后一层的null,倒序遍历就行,碰到第一个非空数就完成了
for (int i = re.size()-1; i >=0 ; i--) {
if (re.get(i)==null){
re.remove(i);
}else {
break;
}
}
System.out.println(Arrays.toString(re.toArray()));
return tn;
}
public void printTreePreOrder(TreeNode treeNode){
if(treeNode!=null){
System.out.println(treeNode.val);
if (treeNode.left!=null){
this.printTreePreOrder(treeNode.left);
}
if (treeNode.right!=null){
this.printTreePreOrder(treeNode.right);
}
}
}
public TreeNode getTree(int[] preorder, int[] inorder) {
int root = preorder[0];
int rootIdx = -1;
for (int i = 0; i < inorder.length; i++) {
if (root == inorder[i]) {
rootIdx = i;
break;
}
}
TreeNode tn = new TreeNode(root);
//根据rootIdx判断是否还有左右子树
if (rootIdx > 0) {
int[] leftTreeIn = new int[rootIdx];
int[] leftTreePre = new int[rootIdx];
System.arraycopy(inorder, 0, leftTreeIn, 0, rootIdx);
System.arraycopy(preorder, 1, leftTreePre, 0, rootIdx);
tn.left = this.getTree(leftTreePre, leftTreeIn);
}
if (rootIdx < inorder.length - 1) {
int[] rightTreeIn = new int[inorder.length - 1 - rootIdx];
int[] rightTreePre = new int[inorder.length - 1 - rootIdx];
System.arraycopy(inorder, rootIdx + 1, rightTreeIn, 0, inorder.length - 1 - rootIdx);
System.arraycopy(preorder, rootIdx + 1, rightTreePre, 0, inorder.length - 1 - rootIdx);
tn.right = this.getTree(rightTreePre,rightTreeIn);
}
return tn;
}