Skip to content

二叉树 - 相同的树 #17

Description

@YalongYan

题目地址: 相同的树

思路

两个树相同,必须 val相同,左右节点也相同
递归判断子树

代码

var isSameTree = function(p, q) {
    if(!p && !q) return true
    if (p && q && p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) {
        return true
    }
    return false
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions