LeetCode 题解(223) : Symmetric Tree

本文详细介绍了使用递归和迭代方法解决判断二叉树是否为镜像的问题,提供了C++、Java和Python三种语言的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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 you could solve it both recursively and iteratively.

题解:

循环写的比较难看。

C++版:

/**
 * Definition for a binary tree node.
 * 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 == NULL || (root->left == NULL && root->right == NULL))
            return true;
        return isSymmetric(root->left, root->right);
    }
    
    bool isSymmetric(TreeNode* first, TreeNode* second) {
        if((first == NULL && second != NULL) || (first != NULL && second == NULL))
            return false;
        if(first == NULL && second == NULL)
            return true;
        if(first->val == second->val) {
            return isSymmetric(first->left, second->right) && isSymmetric(first->right, second->left);
        }
        return false;
    }
};

Java版:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null || (root.left == null && root.right == null))
            return true;
        return isSymmetric(root.left, root.right);
    }
    
    public boolean isSymmetric(TreeNode first, TreeNode second) {
        if(first == null && second != null)
            return false;
        if(first != null && second == null)
            return false;
        if(first == null && second == null)
            return true;
        if(first.val == second.val) {
            return isSymmetric(first.left, second.right) && isSymmetric(first.right, second.left);
        }
        return false;
    }
}

Python版:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import copy

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None or (root.left == None and root.right == None):
            return True
        if root.left != None and root.right != None and root.left.val == root.right.val:
            ql1, ql2, qr1, qr2 = [root.left], [], [root.right], []
            while len(ql1) != 0 or len(qr1) != 0:
                for i in range(len(ql1)):
                    if ql1[i].left != None and qr1[-1 - i].right != None:
                        if ql1[i].left.val != qr1[-1 - i].right.val:
                            return False
                    if (ql1[i].left != None and qr1[-1 - i].right == None) or (ql1[i].left == None and qr1[-1 - i].right != None):
                        return False
                    if ql1[i].right != None and qr1[-1 - i].left != None:
                        if ql1[i].right.val != qr1[-1 - i].left.val:
                            return False
                    if (ql1[i].right != None and qr1[-1 - i].left == None) or (ql1[i].right == None and qr1[-1 - i].left != None):
                        return False
                for i in ql1:
                    if i.left != None:
                        ql2.append(i.left)
                    if i.right != None:
                        ql2.append(i.right)
                        
                for i in qr1:
                    if i.left != None:
                        qr2.append(i.left)
                    if i.right != None:
                        qr2.append(i.right)
                ql1 = copy.copy(ql2)
                qr1 = copy.copy(qr2)
                ql2 = []
                qr2 = []
            return True
        else:
            return False
                
                
                


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值