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.

分析:

不要忘记a.val == b.val这个条件

解法:

public class Solution {
    /**
     * @param a, b, the root of binary trees.
     * @return true if they are tweaked identical, or false.
     */
    public boolean 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;
        }
        return (isTweakedIdentical(a.left, b.left) && isTweakedIdentical(a.right, b.right) ) || (isTweakedIdentical(a.left, b.right) && isTweakedIdentical(a.right, b.left) );
    }
}

results matching ""

    No results matching ""