Flip Equivalent Binary Trees

Jan 05, 2020

第56天。

今天的题目是Flip Equivalent Binary Trees:

简单题,先序遍历判断当前节点的值是否相等,如果不相等则返回false,如果相等的话,判断两个子树是否filpEquiv

bool flipEquiv(TreeNode* root1, TreeNode* root2) {
    if (!root1 && !root2) return true;
    else if (root1 && root2 && root1->val == root2->val) {
        return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right))
                || (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left));    
    } else return false;
}
LeetCodeLeetCode

Asteroid Collision

Maximum Difference Between Node and Ancestor

comments powered by Disqus