(TreeNode r1, TreeNode r2)
| 18 | * rooted at r2 as a subtree somewhere within it. |
| 19 | */ |
| 20 | public static boolean subTree(TreeNode r1, TreeNode r2) { |
| 21 | if (r1 == null) |
| 22 | return false; // big tree empty & subtree still not found. |
| 23 | if (r1.data == r2.data) { |
| 24 | if (matchTree(r1,r2)) return true; |
| 25 | } |
| 26 | return (subTree(r1.left, r2) || subTree(r1.right, r2)); |
| 27 | } |
| 28 | |
| 29 | /* Checks if the binary tree rooted at r1 contains the |
| 30 | * binary tree rooted at r2 as a subtree starting at r1. |