Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对称二叉树 #464

Open
Pcjmy opened this issue Feb 12, 2023 · 1 comment
Open

对称二叉树 #464

Pcjmy opened this issue Feb 12, 2023 · 1 comment

Comments

@Pcjmy
Copy link
Contributor

Pcjmy commented Feb 12, 2023

No description provided.

@floatGray
Copy link

给你一个二叉树的根节点 root , 检查它是否轴对称。
输入:root = [1,2,2,3,4,4,3]
输出:true

const isSymmetric = (root) => {
  
    const check = (left, right) => {
        if (left == null && right == null) { // 两个子树都为null,是对称的
           return true;
        }
        if (left && right) { // 两个子树都存在,则需要:root值相同,且他们的子树也满足镜像
            return left.val == right.val && check(left.left, right.right) && check(left.right, right.left);
        }
        return false;        // 一个子树存在一个不存在,肯定不对称
    };

    if (root == null) {     // 如果传入的root就是null,对称
        return true;
    }           
    return check(root.left, root.right); // 否则,判断它的左右子树是否满足对称
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants