1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public boolean isSameTree(TreeNode p, TreeNode q) { 12 // Start typing your Java solution below 13 // DO NOT write main() function 14 if (p == null && q == null) 15 return true; 16 else if (p != null && q != null) 17 { 18 if (p.val == q.val) 19 { 20 if (isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) 21 return true; 22 else 23 return false; 24 } 25 else 26 return false; 27 } 28 else 29 return false; 30 } 31 }
吐槽:JAVA的语法还是不熟悉。