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

Method covers

java/Chapter 4/Question4_7/Question.java:11–19  ·  view source on GitHub ↗
(TreeNode root, TreeNode p, TreeNode q)

Source from the content-addressed store, hash-verified

9
10 // Checks how many �special� nodes are located under this root
11 public static int covers(TreeNode root, TreeNode p, TreeNode q) {
12 int ret = NO_NODES_FOUND;
13 if (root == null) return ret;
14 if (root == p || root == q) ret += 1;
15 ret += covers(root.left, p, q);
16 if(ret == TWO_NODES_FOUND) // Found p and q
17 return ret;
18 return ret + covers(root.right, p, q);
19 }
20
21 public static TreeNode commonAncestor(TreeNode root, TreeNode p, TreeNode q) {
22 if (q == p && (root.left == q || root.right == q)) return root;

Callers 1

commonAncestorMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected