@@ -5,21 +5,21 @@ struct node* build123(){
5
5
root = insert (root , 2 );
6
6
root = insert (root , 1 );
7
7
root = insert (root , 3 );
8
- return (root );
8
+ return (root );
9
9
}
10
10
11
11
struct node * buildN (int n ){
12
12
struct node * root = NULL ;
13
13
for (int i = 1 ; i <=n ; i ++ ) {
14
14
insert (root , i );
15
15
}
16
- return (root );
16
+ return (root );
17
17
}
18
18
19
19
/*
20
20
Compute the number of nodes in a tree.
21
21
*/
22
- int size (struct node * node ) {
22
+ int size (struct node * node ) {
23
23
if ( node == NULL ) {
24
24
return 0 ;
25
25
}
@@ -46,78 +46,77 @@ int maxDepth(struct node* node) {
46
46
return the minimum data value found in that tree.
47
47
Note that the entire tree does not need to be searched.
48
48
*/
49
- int minValue (struct node * node ) {
49
+ int minValue (struct node * node ) {
50
50
struct node * current = node ;
51
51
// loop down to find the leftmost leaf
52
52
while (current -> left != NULL ) {
53
53
current = current -> left ;
54
54
}
55
- return (current -> data );
55
+ return (current -> data );
56
56
}
57
57
58
58
/*
59
59
Given a non-empty binary search tree,
60
60
return the maximum data value found in that tree.
61
61
Note that the entire tree does not need to be searched.
62
62
*/
63
- int maxValue (struct node * node ) {
63
+ int maxValue (struct node * node ) {
64
64
struct node * current = node ;
65
65
// loop down to find the leftmost leaf
66
66
while (current -> right != NULL ) {
67
67
current = current -> right ;
68
68
}
69
- return (current -> data );
69
+ return (current -> data );
70
70
}
71
71
72
72
/*
73
73
Given a binary search tree, print out
74
74
its data elements in increasing
75
75
sorted order.
76
- This is known as an "inorder" traversal of the tree.
76
+ This is known as an "inorder" traversal of the tree.
77
77
*/
78
- void printTree (struct node * node ) {
78
+ void printTree (struct node * node ) {
79
79
if (node == NULL ) return ;
80
80
printTree (node -> left );
81
81
printf ("%d " , node -> data );
82
- printTree (node -> right );
82
+ printTree (node -> right );
83
83
}
84
84
85
85
/*
86
- Given a binary tree, print its nodes according to the "bottom-up" postorder
86
+ Given a binary tree, print its nodes according to the "bottom-up" postorder
87
87
traversal.
88
- -- both subtrees of a node are printed out completely before the node itself
89
- is printed, and each left subtree is printed before the right subtree.
88
+ -- both subtrees of a node are printed out completely before the node itself
89
+ is printed, and each left subtree is printed before the right subtree.
90
90
*/
91
- void printPostorder (struct node * node ) {
91
+ void printPostorder (struct node * node ) {
92
92
if (node == NULL ) return ;
93
93
printPostorder (node -> left );
94
94
printPostorder (node -> right );
95
95
printf ("%d " , node -> data );
96
96
}
97
97
98
- /*
99
- Given a binary tree and a sum, return true if the tree has a root-to-leaf
100
- path such that adding up all the values along the path equals the given sum.
98
+ /*
99
+ Given a binary tree and a sum, return true if the tree has a root-to-leaf
100
+ path such that adding up all the values along the path equals the given sum.
101
101
Return false if no such path can be found.
102
102
103
103
Strategy: subtract the node value from the sum when recurring down,
104
- and check to see if the sum is 0 when you run out of tree.
104
+ and check to see if the sum is 0 when you run out of tree.
105
105
*/
106
106
int hasPathSum (struct node * node , int sum ) {
107
107
if (node == NULL ) return sum == 0 ;
108
108
else {
109
- return hasPathSum (node -> left , sum - node -> data )
110
- || hasPathSum (node -> right , sum - node -> data )
109
+ return hasPathSum (node -> left , sum - node -> data )
110
+ || hasPathSum (node -> right , sum - node -> data );
111
111
}
112
112
}
113
113
114
- /*
115
- Given a binary tree, print out all of its root-to-leaf
116
- paths, one per line. Uses a recursive helper to do the work.
117
- */
118
- void printPaths (struct node * node ) {
119
- int path [1000 ];
120
- printPathsRecur (node , path , 0 );
114
+ // Utility that prints out an array on a line.
115
+ void printArray (int ints [], int len ) {
116
+ int i ;
117
+ for (i = 0 ; i < len ; i ++ )
118
+ printf ("%d " , ints [i ]);
119
+ printf ("\n" );
121
120
}
122
121
123
122
/*
@@ -140,18 +139,20 @@ void printPathsRecur(struct node* node, int path[], int pathLen) {
140
139
printPathsRecur (node -> right , path , pathLen );
141
140
}
142
141
}
143
-
144
- // Utility that prints out an array on a line.
145
- void printArray ( int ints [], int len ) {
146
- int i ;
147
- for ( i = 0 ; i < len ; i ++ )
148
- printf ( "%d " , ints [ i ]) ;
149
- printf ( "\n" );
142
+ /*
143
+ Given a binary tree, print out all of its root-to-leaf
144
+ paths, one per line. Uses a recursive helper to do the work.
145
+ */
146
+ void printPaths ( struct node * node ) {
147
+ int path [ 1000 ] ;
148
+ printPathsRecur ( node , path , 0 );
150
149
}
151
150
151
+
152
+
152
153
/*
153
- Change a tree so that the roles of the left and right pointers are swapped
154
- at every node.
154
+ Change a tree so that the roles of the left and right pointers are swapped
155
+ at every node.
155
156
*/
156
157
void mirror (struct node * node ) {
157
158
if (node == NULL ) return ;
@@ -164,12 +165,12 @@ void mirror(struct node* node) {
164
165
struct node * prevRight = node -> right ;
165
166
node -> right = node -> left ;
166
167
node -> left = prevRight ;
167
-
168
+
168
169
}
169
170
170
171
/*
171
- For each node in a binary search tree, create a new duplicate node,
172
- and insert the duplicate as the left child of the original node.
172
+ For each node in a binary search tree, create a new duplicate node,
173
+ and insert the duplicate as the left child of the original node.
173
174
*/
174
175
void doubleTree (struct node * node ) {
175
176
if (node == NULL ) return ;
@@ -184,16 +185,16 @@ void doubleTree(struct node* node) {
184
185
node -> left -> left = prevLeft ;
185
186
}
186
187
187
- /*
188
+ /*
188
189
Given two binary trees, return true if they are structurally
189
- identical -- they are made of nodes with the same values
190
- arranged in the same way.
190
+ identical -- they are made of nodes with the same values
191
+ arranged in the same way.
191
192
*/
192
193
int sameTree (struct node * a , struct node * b ) {
193
194
if ( a == NULL && b == NULL ) // both empty
194
- return true;
195
+ return true;
195
196
else if ( a != NULL && b != NULL ) // both non-empty, so compare
196
- return ( (a -> data == b -> data ) &&
197
+ return ( (a -> data == b -> data ) &&
197
198
sameTree (a -> left , b -> left ) &&
198
199
sameTree (a -> right , b -> right ) );
199
200
else
@@ -230,7 +231,7 @@ int countTrees(int numKeys) {
230
231
231
232
return (sum );
232
233
}
233
- }
234
+ }
234
235
235
236
int isBST (struct node * node ) {
236
237
if (node == NULL ) return (true);
@@ -240,7 +241,7 @@ int isBST(struct node* node) {
240
241
// (bug -- an earlier version had min/max backwards here)
241
242
if (node -> left != NULL && maxValue (node -> left ) > node -> data )
242
243
return (false);
243
-
244
+
244
245
// false if the min of the right is <= than us
245
246
if (node -> right != NULL && minValue (node -> right ) <= node -> data )
246
247
return (false);
@@ -253,19 +254,11 @@ int isBST(struct node* node) {
253
254
return true;
254
255
}
255
256
256
- /*
257
- Returns true if the given tree is a binary search tree
258
- (efficient version).
259
- */
260
- int isBST2 (struct node * node ) {
261
- return (isBSTRecur (node , INT_MIN , INT_MAX ));
262
- }
263
-
264
257
/*
265
258
Returns true if the given tree is a BST and its
266
259
values are >= min and <= max.
267
260
*/
268
- int isBSTRecur (struct node * node , int min , int max ) {
261
+ int isBSTRecur (struct node * node , int min , int max ) {
269
262
if (node == NULL ) return (true);
270
263
if (min > node -> data )
271
264
return (false);
@@ -276,4 +269,11 @@ int isBSTRecur(struct node* node, int min, int max) {
276
269
// false if, recursively, the left or right is not a BST
277
270
return (isBSTRecur (node -> left , min , node -> data ) &&
278
271
isBSTRecur (node -> right , node -> data + 1 , max )); // note the +1
279
- }
272
+ }
273
+ /*
274
+ Returns true if the given tree is a binary search tree
275
+ (efficient version).
276
+ */
277
+ int isBST2 (struct node * node ) {
278
+ return (isBSTRecur (node , INT_MIN , INT_MAX ));
279
+ }
0 commit comments