【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:
    bool isSymmetric(TreeNode* root) {
        return isMirror(root, root);
    }

    bool isMirror(TreeNode *root, TreeNode *copy) {
        if (!root && !copy) return true;
        if (!root || !copy) return false;
        if (root->val == copy->val) {
            return isMirror(root->left, copy->right) && isMirror(root->right, copy->left);
        }
        return false;
    }
};

思路二:迭代

将树的左右节点按相关顺序插入队列中,判断队列中每两个节点是否相等。

代码

时间复杂度:O(n)
空间复杂度:O(n)

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        queue<TreeNode*> que;
        que.push(root);
        que.push(root);
        while (!que.empty()) {
            TreeNode *node1 = que.front();
            que.pop();
            TreeNode *node2 = que.front();
            que.pop();
            if (!node1 && !node2) continue;
            if (!node1 || !node2 || node1->val != node2->val) return false;
            que.push(node1->left);
            que.push(node2->right);
            que.push(node1->right);
            que.push(node2->left);
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/galaxy-hao/p/12359371.html

时间: 2024-08-30 15:46:16

【LeetCode】101. 对称二叉树的相关文章

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.对称二叉树 - JavaScript

题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / 2 2 / \ / 3 4 4 3 解法 1:递归检查 根据题目"对称"的定义,递归过程如下: 对称节点的 val 是否相同 依次递归对称节点的 left1 和 right2.right1 和 left2(结合上面的例子更好理解) 代码实现如下: // ac地址:https://leetcode-cn.com/problems/symmetric-tree/ // 原文地

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

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: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

剑指offer (19) 二叉树镜像 对称二叉树

题目: 输入一个二叉树,输出其镜像. BinTreeNode* ReverseTree(BinTreeNode* pRoot) { if (pRoot == NULL) return NULL; BinTreeNode* pLeftReverse = ReverseTree(pRoot->left); BinTreeNode* pRightReverse = ReverseTree(pRoot->right); pRoot->left = pRightReverse; pRoot->