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.
Notice:
There is no two nodes with the same value in the tree.
Example
1 1
/ \ / \
2 3 and 3 2
/ \
4 4
are identical.
1 1
/ \ / \
2 3 and 3 2
/ /
4 4
are not identical.
publicbooleanisTweakedIdentical(TreeNode a,TreeNode b) {if (a ==null&& b ==null) {returntrue; } elseif (a ==null|| b ==null) {returnfalse; }if (a.val!=b.val) {returnfalse; }boolean LL =isTweakedIdentical(a.left,b.left);boolean RR =isTweakedIdentical(a.right,b.right);if (LL && RR) {returntrue; }boolean LR =isTweakedIdentical(a.left,b.right);boolean RL =isTweakedIdentical(a.right,b.left);if (LR && RL) {returntrue; }returnfalse;}