https://leetcode.com/problems/same-tree/
判斷2個二元樹是否相同,回傳true或false。
從第一個的左節點與第二個的右節點利用遞迴特性相互比較,再由第一個的右節點與第二個的左節點相互比較,最後回傳結果。
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if (p == NULL && q == NULL) {
return true;
} else if (p == NULL && q != NULL) {
return false;
} else if (p != NULL && q == NULL) {
return false;
} else {
if (p->val == q->val){
bool lTree=isSameTree(p->left,q->left);
bool rTree=isSameTree(p->right,q->right);
return (lTree && rTree);
}
else{
return false;
}
}
}
var isSameTree = function(p, q) {
if (p == null && q == null) {
return true;
} else if (p == null && q != null) {
return false;
} else if (p != null && q == null) {
return false;
} else {
if (p.val == q.val){
var lTree=isSameTree(p.left,q.left);
var rTree=isSameTree(p.right,q.right);
return (lTree && rTree);
}
else{
return false;
}
}
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
Cleverness is a gift, and kindness is a choice.
聰明是一種天賦,而善良是一種選擇。