【二叉树是否对称】Symmetric Tree

34 篇文章 0 订阅
29 篇文章 0 订阅

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.

题意:判断是否为对称数

解法一:性能不怎么好的递归,先按原树创建一个镜像再比较两棵树是否一致

若要加强的话,可以直接在原树上比较,递归比较左右节点的值是否相同

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public TreeNode mirror(TreeNode root){
        if(root == null) return null;
        TreeNode left = mirror(root.left);
        TreeNode right = mirror(root.right);
        TreeNode nr = new TreeNode(root.val);
        nr.left = right;
        nr.right = left;
        return nr;
    }
    
    public boolean isSame(TreeNode p1, TreeNode p2){
        if(p1==null && p2==null) return true;
        if(p1 == null && p2 != null) return false;
        if(p1 != null && p2 == null) return false;
        return (p1.val == p2.val) && isSame(p1.left, p2.left) && isSame(p1.right, p2.right);
    }
    
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        TreeNode p = mirror(root);
        return isSame(root, p);
    }
}
解法二:递归,直接比较原树的左右节点值

public class Solution {
    
    public boolean isSame(TreeNode p1, TreeNode p2){
        if(p1==null && p2==null) return true;
        if(p1 == null && p2 != null) return false;
        if(p1 != null && p2 == null) return false;
        return (p1.val == p2.val) && isSame(p1.left, p2.right) && isSame(p1.right, p2.left);
    }
    
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSame(root, root);
    }
}

解法三:使用两个队列分别遍历树的左右节点,并比较值

public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> q1 = new LinkedList<TreeNode>();
        Queue<TreeNode> q2 = new LinkedList<TreeNode>();
        
        if(root.left != null) q1.add(root.left);
        if(root.right != null) q2.add(root.right);
        
        while(!q1.isEmpty() && !q2.isEmpty()){
            TreeNode t1 = q1.remove();
            TreeNode t2 = q2.remove();
            if(t1.val == t2.val){
                if(t1.left != null && t2.right != null){
                     q1.add(t1.left);
                     q2.add(t2.right);
                }//注意判断的条件,只有一个不为空,一个为空才不对称
                else if((t1.left!=null && t2.right==null) || (t1.left==null && t2.right!=null))
                return false;
                if(t1.right != null && t2.left != null){
                    q1.add(t1.right);
                    q2.add(t2.left);
                }
                else if((t1.right !=null && t2.left==null) || (t1.right==null && t2.left!=null))
                return false;
            }
            else{
                return false;
            }
        }
        if(q1.isEmpty() && q2.isEmpty())
        return true;
        
        return false;
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一段C++代码,用来判断给定的二叉树是否对称: ```c++ #include <iostream> #include <cstring> using namespace std; const int MAXN = 1010; char tree[MAXN]; bool isSymmetric(char* left, char* right) { if (*left == '#' && *right == '#') { // 左右子树都为空 return true; } if (*left == '#' || *right == '#') { // 左右子树一个为空,一个不为空 return false; } if (*left != *right) { // 左右子树根节点不同 return false; } return isSymmetric(left + 1, right - 1); // 递归判断左右子树是否对称 } int main() { cin >> tree; int len = strlen(tree); if (len % 2 == 0) { // 如果字符串长度为偶数,肯定不对称 cout << "No" << endl; return 0; } char* left = tree + 1; char* right = tree + len - 2; if (isSymmetric(left, right)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } ``` 该程序首先读入二叉树的顺序存储序列,然后判断字符串长度是否为偶数。如果字符串长度为偶数,肯定不对称,直接输出"No"。否则,定义指针left指向字符串的第2个字符,指针right指向字符串的倒数第2个字符,然后调用isSymmetric函数来判断左右子树是否对称。isSymmetric函数的实现比较简单:如果左右子树都为空,返回true;如果左右子树一个为空,一个不为空,返回false;如果左右子树根节点不同,返回false;否则,递归判断左右子树是否对称。最终,如果二叉树对称,输出"Yes",否则输出"No"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值