Skip to content

Commit 62f23b9

Browse files
committed
SS21
1 parent 741cffb commit 62f23b9

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

preInReconstruct/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany</groupId>
5+
<artifactId>preInReconstruct</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>11</maven.compiler.source>
11+
<maven.compiler.target>11</maven.compiler.target>
12+
</properties>
13+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.mycompany.preinreconstruct;
7+
8+
import java. util. ArrayList;
9+
10+
/**
11+
*
12+
* @author asadp
13+
*/
14+
public class PreInReconstruct {
15+
class Node {
16+
int item;
17+
Node left,right;
18+
19+
public Node(int key) {
20+
item = key;
21+
left = right = null;
22+
}
23+
}
24+
ArrayList<Node> nodes = new ArrayList<Node>();
25+
void preInReconstruct(int preOrder[], int inOrder[]){
26+
Node root = new Node(preOrder[0]);
27+
int index = 0;
28+
for(int i = 0; i < inOrder.length; i++){
29+
if(inOrder[i] == root.item){
30+
index = i;
31+
break;
32+
}
33+
}
34+
int inOrderLeft[] = new int[index];
35+
int inOrderRight[] = new int[inOrder.length - index -1];
36+
for(int i = 0; i < index; i++){
37+
inOrderLeft[i] = inOrder[i];
38+
}
39+
for(int i = 0; i < inOrder.length - index -1; i++){
40+
inOrderRight[i] = inOrder[i + index];
41+
}
42+
int preOrderLeft[] = new int[index];
43+
int preOrderRight[] = new int[inOrder.length - index -1];
44+
for(int i = 0; i < index; i++){
45+
preOrderLeft[i] = preOrder[i + 1];
46+
}
47+
for(int i = 0; i < inOrder.length - index -1; i++){
48+
preOrderRight[i] = inOrder[i + index + 1];
49+
}
50+
51+
root.left = preOrderLeft[0];
52+
53+
}
54+
}

0 commit comments

Comments
 (0)