LeetCode(101):对称二叉树

Easy!

题目描述:

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [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

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分。

解题思路:

判断二叉树是否是平衡树,比如有两个节点n1, n2,我们需要比较n1的左子节点的值和n2的右子节点的值是否相等,同时还要比较n1的右子节点的值和n2的左子结点的值是否相等,以此类推比较完所有的左右两个节点。我们可以用递归和迭代两种方法来实现,写法不同,但是算法核心都一样。

递归方法 (Recursive Solution):

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if (!root) return true;
        return isSymmetric(root->left, root->right);
    }
    bool isSymmetric(TreeNode *left, TreeNode *right) {
        if (!left && !right) return true;
        if (left && !right || !left && right || left->val != right->val) return false;
        return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
    }

};

迭代方法 (Iterative Solution):

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if (!root) return true;
        queue<TreeNode*> q1, q2;
        q1.push(root->left);
        q2.push(root->right);

        while (!q1.empty() && !q2.empty()) {
            TreeNode *node1 = q1.front();
            TreeNode *node2 = q2.front();
            q1.pop();
            q2.pop();
            if((node1 && !node2) || (!node1 && node2)) return false;
            if (node1) {
                if (node1->val != node2->val) return false;
                q1.push(node1->left);
                q1.push(node1->right);
                q2.push(node2->right);
                q2.push(node2->left);
            }
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/ariel-dreamland/p/9159806.html

时间: 2024-08-29 19:51:54

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

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