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

相同的树-100 #21

Open
sl1673495 opened this issue May 8, 2020 · 1 comment
Open

相同的树-100 #21

sl1673495 opened this issue May 8, 2020 · 1 comment
Labels
BFS 广度优先遍历 DFS 深度优先遍历 二叉树

Comments

@sl1673495
Copy link
Owner

sl1673495 commented May 8, 2020

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

输出: true
示例 2:

输入:      1          1
          /           \
         2             2

        [1,2],     [1,null,2]

输出: false
示例 3:

输入:       1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

输出: false

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/same-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

DFS

深度优先遍历就是直接递归比较,把 left 和 right 节点也视为一棵树。继续调用 isSameTree 方法。

题解

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
  if (!p && !q) return true;
  if ((p && !q) || (!p && q)) return false;

  if (isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) {
    return p.val === q.val;
  } else {
    return false;
  }
};

BFS

BFS 也是标准的思路,就是把节点放进一个队列里,然后在处理节点的时候遇到有 left 或 right 子节点,就继续放进队列里,下一轮循环继续处理。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
  let queue1 = [p];
  let queue2 = [q];

  while (queue1.length) {
    let node1 = queue1.shift();
    let node2 = queue2.shift();

    if (!node1 || !node2) {
      if (node1 !== node2) {
        return false;
      }
      continue;
    }

    if (node1.val !== node2.val) {
      return false;
    }

    queue1.push(node1.left);
    queue1.push(node1.right);
    queue2.push(node2.left);
    queue2.push(node2.right);
  }

  return true;
};
@sl1673495 sl1673495 added BFS 广度优先遍历 DFS 深度优先遍历 二叉树 labels May 8, 2020
@cwjbjy
Copy link

cwjbjy commented Jul 20, 2022

DFS

var isSameTree = function (p, q) {
    let flag = true;
    const dfs = (p, q) => {
        //节点都不在,return
        if (!p && !q) return;
        //节点都在,进行比较是否相等
        if (p && q) {
            if (p.val !== q.val) return flag = false;
            //节点相等,继续下一层比较
            dfs(p.left, q.left);
            dfs(p.right, q.right);
        } else {
            //节点一个在,一个不在,直接返回false
            return flag = false;
        }
    }
    dfs(p, q);
    return flag;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BFS 广度优先遍历 DFS 深度优先遍历 二叉树
Projects
None yet
Development

No branches or pull requests

2 participants