@@ -149,28 +149,23 @@ else if (t1 != null && t2 != null) {
149
149
public static BTree commonRoot (BTree root , int p , int q ) {
150
150
if (root == null )
151
151
return null ;
152
- if (root .value == p && root .value == q )
152
+ if (root .value == p || root .value == q )
153
153
return root ;
154
154
155
155
BTree l = commonRoot (root .left , p , q );
156
156
BTree r = commonRoot (root .right , p , q );
157
157
158
+ /*
158
159
if (r != null && r.value != q && r.value != p)
159
160
return r;
160
161
if (l != null && l.value != q && l.value != p)
161
162
return l;
162
-
163
- if (root .value == p || root .value == q ) {
164
- if (l != null || r != null )
165
- return root ;
166
-
167
- } else if (l != null && r != null && l .value != r .value )
163
+ */
164
+ if (l != null && r != null && l .value != r .value )
168
165
return root ;
169
166
else
170
167
return (l != null ) ? l : r ;
171
168
172
- return null ;
173
-
174
169
}
175
170
176
171
public static BTree coverNode (BTree root , int p ) {
@@ -406,6 +401,8 @@ public static void main(String[] args) {
406
401
if (cr != null && coverNode (t1 , p ) != null && coverNode (t1 , q ) != null )
407
402
System .out .println (cr .value );
408
403
404
+ p = 12 ;
405
+ q = 13 ;
409
406
BTree cr1 = commonRoot (t1 , p , q );
410
407
System .out .println ("common root: " + p + " and " + q );
411
408
if (cr1 != null && coverNode (t1 , p ) != null && coverNode (t1 , q ) != null )
0 commit comments