File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed
src/main/java/com/mycompany/preinreconstruct Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments