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;否则判断两节点是否相等,若相等则返回它们对应的子节点的比较结果;否则返回false。

迭代做法是维护一个节点队列,每次取出队列头部两个节点比较其是否相等,若不相等且两节点均不为空,则依次把两个节点对称位置的子节点加入到队列中,注意null也要加入到队列;否则返回false

代码

递归:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode* root) {
13         if(root == NULL) return true;
14         return symmetric(root->left, root->right);
15     }
16     bool symmetric(TreeNode* left, TreeNode* right){
17         if(left == right) return true;
18         else if(left && right && left->val == right->val){
19             if(symmetric(left->left, right->right) && symmetric(left->right, right->left))
20                 return true;
21         }
22         return false;
23     }
24 };

迭代:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode* root) {
13         if(root == NULL) return true;
14         queue<TreeNode*> q;
15         q.push(root->left);
16         q.push(root->right);
17         while(q.size()){
18             TreeNode* node1 = q.front();
19             q.pop();
20             TreeNode* node2 = q.front();
21             q.pop();
22             if(node1 && node2){
23                 if(node1->val != node2->val) return false;
24                 else{
25                     q.push(node1->left);
26                     q.push(node2->right);
27                     q.push(node1->right);
28                     q.push(node2->left);
29                 }
30             }
31             else if(node1 || node2) return false;
32         }
33         return true;
34     }
35 };

原文地址:https://www.cnblogs.com/wmx24/p/9766858.html

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

LeetCode 101. 对称二叉树(Symmetric Tree)的相关文章

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

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. 对称二叉树,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的孩子

LeetCode(1) Symmetric Tree

从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 题目分析: 第一道题目简单的题目,

LeetCode之“树”:Symmetric Tree

Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if y

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

对称树 Symmetric Tree

算法能力一直不好,决定利用晚上和假期时间多做点算法题,动动手,提高自己.大家看到我的水平低还望莫要嘲笑,嘻嘻. 这一题呢是LeetCode上的对称树问题, Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the fol