Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a01fac

Browse files
committed
Subtree check done (comments pending)
1 parent e34600d commit 2a01fac

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

‎src/main/java/com/ctci/treesandgraphs/CheckSubtree.java

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,80 @@
66
*/
77
public class CheckSubtree {
88

9-
/*private static boolean isT2SubtreeofT1(TreeNode t1, TreeNode t2) {
10-
9+
private static boolean isT2SubtreeOfT1(TreeNode t1, TreeNode t2) {
10+
if (t1 == null) {
11+
return false;
12+
} else if (t2 == null) {
13+
return true;
14+
}
15+
16+
if (t1.val == t2.val) {
17+
if (matchTree(t1, t2)) {
18+
return true;
19+
}
20+
}
21+
return isT2SubtreeOfT1(t1.left, t2) || isT2SubtreeOfT1(t1.right, t2);
1122
}
12-
23+
1324
private static boolean matchTree(TreeNode a, TreeNode b) {
14-
}*/
15-
25+
if (a == null && b == null) {
26+
return true;
27+
} else if (a == null) {
28+
return false;
29+
} else if (b == null) {
30+
return true;
31+
} else if (a.val != b.val) {
32+
return false;
33+
} else {
34+
return matchTree(a.left, b.left) && matchTree(a.right, b.right);
35+
}
36+
}
37+
1638
public static void main(String[] args) {
39+
/*
40+
The BST looks like:
41+
42+
4
43+
/ \
44+
2 8
45+
/ \ / \
46+
1 3 6 9
47+
/
48+
0
49+
50+
*/
51+
TreeNode treeRoot = new TreeNode(4);
52+
treeRoot.left = new TreeNode(2);
53+
treeRoot.right = new TreeNode(8);
54+
treeRoot.left.left = new TreeNode(1);
55+
treeRoot.left.right = new TreeNode(3);
56+
treeRoot.left.left.left = new TreeNode(0);
57+
treeRoot.right.left = new TreeNode(6);
58+
treeRoot.right.right = new TreeNode(9);
59+
System.out.println(isT2SubtreeOfT1(treeRoot, treeRoot));
60+
System.out.println(isT2SubtreeOfT1(treeRoot, treeRoot.left));
61+
System.out.println(isT2SubtreeOfT1(treeRoot, treeRoot.right));
62+
63+
/*
64+
The sub-tree:
65+
66+
8
67+
/
68+
6
69+
*/
70+
TreeNode treeRoot2 = new TreeNode(8);
71+
treeRoot2.left = new TreeNode(6);
72+
System.out.println(isT2SubtreeOfT1(treeRoot, treeRoot2));
73+
74+
/*
75+
The sub-tree:
76+
77+
8
78+
/
79+
9
80+
*/
81+
TreeNode treeRoot3 = new TreeNode(8);
82+
treeRoot3.left = new TreeNode(9);
83+
System.out.println(isT2SubtreeOfT1(treeRoot, treeRoot3));
1784
}
18-
}
85+
}

‎src/main/java/com/leetcode/trie/LongestWord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class LongestWord {
1414
private class TrieNode {
1515
char ch;
1616
HashMap<Character, TrieNode> children = new HashMap<>();
17-
String completeWord; // to mark a complete word in the tri data structure
17+
String completeWord; // to mark a complete word in the trie data structure
1818

1919
TrieNode(char ch) {
2020
this.ch = ch;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /