MCPcopy Create free account
hub / github.com/careercup/ctci / matchTree

Method matchTree

java/Chapter 4/Question4_8/Question.java:32–41  ·  view source on GitHub ↗
(TreeNode r1, TreeNode r2)

Source from the content-addressed store, hash-verified

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

Callers 1

subTreeMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected