14
14
* }
15
15
*/
16
16
class Solution {
17
- public TreeNode buildTree (int [] preorder , int [] inorder ) {
18
- if (preorder .length == 0 || inorder .length == 0 ) return null ;
19
-
20
- TreeNode root = new TreeNode (preorder [0 ]);
21
- int mid = 0 ;
22
- for (int i = 0 ; i < inorder .length ; i ++) {
23
- if (preorder [0 ] == inorder [i ]) mid = i ;
24
- }
25
-
26
- root .left = buildTree (Arrays .copyOfRange (preorder , 1 , mid + 1 ), Arrays .copyOfRange (inorder , 0 , mid ));
27
- root .right = buildTree (Arrays .copyOfRange (preorder , mid + 1 , preorder .length ), Arrays .copyOfRange (inorder , mid + 1 , inorder .length ));
28
-
29
- return root ;
17
+
18
+ public TreeNode buildTree (int [] preorder , int [] inorder ) {
19
+ if (preorder .length == 0 || inorder .length == 0 ) return null ;
20
+
21
+ TreeNode root = new TreeNode (preorder [0 ]);
22
+ int mid = 0 ;
23
+ for (int i = 0 ; i < inorder .length ; i ++) {
24
+ if (preorder [0 ] == inorder [i ]) mid = i ;
30
25
}
26
+
27
+ root .left =
28
+ buildTree (
29
+ Arrays .copyOfRange (preorder , 1 , mid + 1 ),
30
+ Arrays .copyOfRange (inorder , 0 , mid )
31
+ );
32
+ root .right =
33
+ buildTree (
34
+ Arrays .copyOfRange (preorder , mid + 1 , preorder .length ),
35
+ Arrays .copyOfRange (inorder , mid + 1 , inorder .length )
36
+ );
37
+
38
+ return root ;
39
+ }
31
40
}
32
41
33
42
// Solution without using Array copies
34
43
class Solution {
35
44
36
- Map <Integer , Integer > inorderPositions = new HashMap <>();
45
+ Map <Integer , Integer > inorderPositions = new HashMap <>();
37
46
38
- public TreeNode buildTree (int [] preorder , int [] inorder ) {
39
- if (preorder .length < 1 || inorder .length < 1 ) return null ;
47
+ public TreeNode buildTree (int [] preorder , int [] inorder ) {
48
+ if (preorder .length < 1 || inorder .length < 1 ) return null ;
40
49
41
- for (int i = 0 ; i < inorder .length ; i ++) {
42
- inorderPositions .put (inorder [i ], i );
43
- }
44
-
45
- return builder (preorder , 0 , 0 , inorder .length -1 );
50
+ for (int i = 0 ; i < inorder .length ; i ++) {
51
+ inorderPositions .put (inorder [i ], i );
46
52
}
47
53
48
- public TreeNode builder (int [] preorder , int preorderIndex , int inorderLow , int inorderHigh ) {
49
- if ( preorderIndex > preorder . length - 1 || inorderLow > inorderHigh ) return null ;
54
+ return builder (preorder , 0 , 0 , inorder . length - 1 );
55
+ }
50
56
51
- int currentVal = preorder [preorderIndex ];
52
- TreeNode n = new TreeNode (currentVal );
53
- int mid = inorderPositions .get (currentVal );
57
+ public TreeNode builder (
58
+ int [] preorder ,
59
+ int preorderIndex ,
60
+ int inorderLow ,
61
+ int inorderHigh
62
+ ) {
63
+ if (
64
+ preorderIndex > preorder .length - 1 || inorderLow > inorderHigh
65
+ ) return null ;
54
66
55
- n .left = builder (preorder , preorderIndex +1 , inorderLow , mid -1 );
56
- n .right = builder (preorder , preorderIndex + (mid - inorderLow ) + 1 , mid +1 , inorderHigh );
67
+ int currentVal = preorder [preorderIndex ];
68
+ TreeNode n = new TreeNode (currentVal );
69
+ int mid = inorderPositions .get (currentVal );
57
70
58
- return n ;
59
- }
71
+ n .left = builder (preorder , preorderIndex + 1 , inorderLow , mid - 1 );
72
+ n .right =
73
+ builder (
74
+ preorder ,
75
+ preorderIndex + (mid - inorderLow ) + 1 ,
76
+ mid + 1 ,
77
+ inorderHigh
78
+ );
60
79
61
- }
80
+ return n ;
81
+ }
82
+ }
0 commit comments