My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
else if (p == null || q == null)
return false;
return dfs(p, q);
}
private boolean dfs(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;
else
return dfs(p.left, q.left) && dfs(p.right, q.right);
}
}
My test result:
这次题目比较简单吧。。递归,没什么好总结的。
后天就要走了,昨天喝酒差点哭。。。
感觉我爸的这几个朋友,对我真的很好。直接的说,家里的亲戚呆在一起的时间还没有和他们一起的时间多。挺感谢他们的,昨天也表现得挺好了,尽心了。
**
总结: DFS, Recursion
**
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q != null) {
return false;
}
else if (p != null && q == null) {
return false;
}
else if (p == null && q == null) {
return true;
}
else if (p.val != q.val) {
return false;
}
else {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}
recursion
比较简单,没什么好说的。
Anyway, Good luck, Richardo! -- 08/28/2016