| 30 | * binary tree rooted at r2 as a subtree starting at r1. |
| 31 | */ |
| 32 | public static boolean matchTree(TreeNode r1, TreeNode r2) { |
| 33 | if (r2 == null && r1 == null) |
| 34 | return true; // nothing left in the subtree |
| 35 | if (r1 == null || r2 == null) |
| 36 | return false; // big tree empty & subtree still not found |
| 37 | if (r1.data != r2.data) |
| 38 | return false; // data doesn�t match |
| 39 | return (matchTree(r1.left, r2.left) && |
| 40 | matchTree(r1.right, r2.right)); |
| 41 | } |
| 42 | |
| 43 | public static void main(String[] args) { |
| 44 | // t2 is a subtree of t1 |