LeetCode 101.对称二叉树 - JavaScript

题目描述:给定一个二叉树,检查它是否是镜像对称的。

题目分析

下面这种二叉树就是镜像对称的,符合题目要求:

    1
   /   2   2
 / \ / 3  4 4  3

解法 1:递归检查

根据题目“对称”的定义,递归过程如下:

  • 对称节点的 val 是否相同
  • 依次递归对称节点的 left1 和 right2、right1 和 left2(结合上面的例子更好理解)

代码实现如下:

// ac地址:https://leetcode-cn.com/problems/symmetric-tree/
// 原文地址:https://xxoo521.com/2020-02-16-symmetric-tree/

/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
    if (!root) return true;

    return __isSymmetric(root.left, root.right);
};

function __isSymmetric(t1, t2) {
    if (!t1 && !t2) return true;
    if (!t1 || !t2) return false;

    return (
        t1.val === t2.val &&
        __isSymmetric(t1.left, t2.right) &&
        __isSymmetric(t1.right, t2.left)
    );
}

时间复杂度是 O(N),空间复杂度是 O(N)。因为最坏情况下,树是线性的。

更多资料

原文地址:https://www.cnblogs.com/geyouneihan/p/12319384.html

时间: 2024-08-30 13:08:19

LeetCode 101.对称二叉树 - JavaScript的相关文章

Leetcode 101.对称二叉树

对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 1 class Solution{ 2 public: 3 bool isSymmetric(TreeNode* root){ 4 if(root==NUL

LeetCode 101. 对称二叉树(Symmetric Tree)

题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / 2 2 / \ / 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / 2 2 \ 3 3 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 解题思路 本题可用递归和迭代两种做法来求解. 递归做法是每次对于对称的两个节点,首先判断是否都为空,若都为空则返回true:否则判断两节点是否相等,若相等则返回它们对应的子节

leetcode——101. 对称二叉树

class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root:return True def Tree(p,q): if not p and not q:return True if p and q and p.val==q.val: return Tree(p.left,q.right) and

101. 对称二叉树,c++迭代递归解法

101. 对称二叉树,c++迭代递归解法 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树?[1,2,2,3,4,4,3] 是对称的. 1 / 2 2 / \ / 3 4 4 3 但是下面这个?[1,2,2,null,3,null,3] 则不是镜像对称的: 1 / 2 2 \ 3 3 这道题可以用迭代和递归两种方法求解 迭代法代码如下,主要思想是,将树的左右分支放入两个队列中.因为题目是判断两个数是否对称,所以在将节点a的孩子放左队列时,先放a的右子结点,再放a的左子结点:在将节点b的孩子

2.(101)对称二叉树

2.(101)对称二叉树 2020年3月20日 Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,nu

【LeetCode】101. 对称二叉树

题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / 2 2 / \ / 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / 2 2 \ 3 3 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 本题同[剑指Offer]面试题28. 对称的二叉树 思路一:递归 利用镜像. 代码 时间复杂度:O(n) 空间复杂度:O(n) class Solution { public:

101. 对称二叉树

给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/symmetric-tree 1 public class Symmetr

LeetCode 297.序列化二叉树 - JavaScript

题目描述 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实现二叉树的序列化与反序列化.这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构. 说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的. 序列化二叉树思路 使用广度优先(

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return