【一次过】Lintcode 468. 对称二叉树

本文探讨了如何判断一棵二叉树是否为对称二叉树,提供了递归和非递归两种方法的详细解析。通过对例题的分析,阐述了解题思路,包括通过比较左右子树是否互为镜像的递归方法,以及利用队列进行广度优先搜索的非递归方法。

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

给你一颗二叉树,判断是否是对称二叉树

样例

例1:

输入:{1,2,2,3,4,4,3}
输出:true
说明:

        1
        / \
       2  2
      / \ / \
     3 4 4 3

是一个对称的二叉树。

例2:

输入:{1,2,2,#,3,#,3}
输出:false
说明:

        1
        / \
       2  2
        \   \
         3   3

不是对称的二叉树。

挑战

你能递归地和迭代地解决它吗?


解题思路1:

递归。类似于Lintcode 469. Same Tree
node1的左子树是否等于node2的右子树 && node1的右子树是否等于node2的左子树

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root of binary tree.
     * @return: true if it is a mirror of itself, or false.
     */
    public boolean isSymmetric(TreeNode root) {
        // write your code here
        if(root == null)
            return true;
        
        return helper(root.left, root.right);
    }
    
    private boolean helper(TreeNode nodeLeft, TreeNode nodeRight){
        if(nodeLeft == null && nodeRight == null)
            return true;
        else if(nodeLeft == null || nodeRight == null)
            return false;
        
        if(nodeLeft.val != nodeRight.val)
            return false;
            
        return helper(nodeLeft.left, nodeRight.right) && helper(nodeLeft.right, nodeRight.left);
    }
}

解题思路2:

非递归BFS使用Queue来保存成对的节点
     1.出队的时候也是成对成对的 
               1.若都为空,继续;
                2.一个为空,返回false;
                3.不为空,比较当前值,值不等,返回false;
      2.确定入队顺序,每次入队都是成对成对的,如left.left, right.right ;left.rigth,right.left

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.*;
public class Solution {
    boolean isSymmetrical(TreeNode root){
        if(root == null)
            return true;
        
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty()){
            //成对取出
            TreeNode node1 = queue.poll();
            TreeNode node2 = queue.poll();
            
            if(node1 == null && node2 == null)
                continue;
            if(node1 == null || node2 == null)
                return false;
            if(node1.val != node2.val)
                return false;
            
            //成对插入
            queue.offer(node1.left);
            queue.offer(node2.right);
            queue.offer(node1.right);
            queue.offer(node2.left);
        }
        
        return true;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值