forked from surajr/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSubtree.java
More file actions
31 lines (28 loc) · 732 Bytes
/
isSubtree.java
File metadata and controls
31 lines (28 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t)
{
String tree1 = preorder(s, true);
String tree2 = preorder(t, true);
return tree1.indexOf(tree2) >= 0;
}
public String preorder(TreeNode t, boolean left)
{
if(t == null)
{
if(left)
return "lnull";
else
return "rnull";
}
return "#" + t.val + " " + preorder(t.left, true) + " " + preorder(t.right, true);
}
}