Tweaked Identical Binary Tree
题目:
Check two given binary trees are identical or not. Assuming any number of tweaks are allowed. A tweak is defined as a swap of the children of one node in the tree.
分析:
真心要认真读题目,人家题目说的是tweak多少次都可以接受。
解法:
class Solution {
public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are tweaked identical, or false.
*/
bool isTweakedIdentical(TreeNode* a, TreeNode* b) {
// Write your code here
if (a == NULL && b == NULL) {
return true;
}
if (a == NULL || b == NULL) {
return false;
}
if (a->val != b->val) {
return false;
}
bool left = (isTweakedIdentical(a->left, b->right) || isTweakedIdentical(a->left, b->left) );
bool right = (isTweakedIdentical(a->right, b->left) || isTweakedIdentical(a->right, b->right) );
return (left && right);
}
};