diff options
-rw-r--r-- | SameTree.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/SameTree.java b/SameTree.java new file mode 100644 index 0000000..50119b9 --- /dev/null +++ b/SameTree.java @@ -0,0 +1,27 @@ +import java.lang.*; +import java.util.*; +import node.*; + +class SameTree { + public static boolean isSame(TreeNode p, TreeNode q) { + if (p == null && q == null) + return true; + else if (p == null || q == null) + return false; + else if (p.val != q.val) + return false; + boolean l = isSame(p.left, q.left); + boolean r = isSame(p.right, q.right); + return l && r; + } + + public static void main(String[] args) { + TreeNode leftLeaf = new TreeNode(9); + TreeNode rightLeaf = new TreeNode(7); + TreeNode root = new TreeNode(3, leftLeaf, rightLeaf); + TreeNode leftClone = new TreeNode(9); + TreeNode rightClone = new TreeNode(9); + TreeNode clone = new TreeNode(3, leftClone, rightClone); + System.out.println(isSame(root, clone)); + } +} |