【一次过】Lintcode 1360. 对称树

本文介绍了一种算法问题,即如何判断一棵二叉树是否为其自身的镜像(即是否对称)。提供了两种解决方案,一种是递归方法,通过翻转子树并比较;另一种是迭代方法,利用队列进行广度优先遍历比较。

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

给定二叉树,返回它是否是自身的镜像(即这棵二叉树是否对称)。

样例

样例1

输入: {1,2,2,3,4,4,3}
输出: true
解释:
    1
   / \
  2   2
 / \ / \
3  4 4  3
{1,2,2,3,4,4,3}这棵二叉树是对称的

样例2

输入: {1,2,2,#,3,#,3}
输出: false
解释:
    1
   / \
  2   2
   \   \
   3    3
很显然这棵二叉树并不对称

挑战

用递归和迭代的方法来解决这个问题(2种解法)


解题思路1:

    递归。判断是否对称,可以先将其中一子树翻转(类似于Lintcode 175. 翻转二叉树),然后看两子树是否相等(类似于Lintcode 469. Same Tree

/**
 * 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: root of the given tree
     * @return: whether it is a mirror of itself 
     */
    public boolean isSymmetric(TreeNode root) {
        // Write your code here
        if(root == null)
            return true;
        
        reverseTree(root.right);
        
        return isSame(root.left, root.right);
    }
    
    private void reverseTree(TreeNode root){
        if(root == null)
            return;
        
        reverseTree(root.left);
        reverseTree(root.right);
        
        TreeNode tempLeft = root.left;
        TreeNode tempRight = root.right;
        root.left = tempRight;
        root.right = tempLeft;
    }
    
    private boolean isSame(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null)
            return true;
        else if(root1 == null || root2 == null)
            return false;
        
        if(isSame(root1.left, root2.left) && isSame(root1.right, root2.right))
            return root1.val == root2.val ? true : false;
            
        return false;
    }
}

解题思路2:

    循环,使用辅助数据结构--队列。类似于广度优先遍历。

/**
 * 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: root of the given tree
     * @return: whether it is a mirror of itself 
     */
    public boolean isSymmetric(TreeNode root) {
        // Write your code here
        if(root == null)
            return true;
        
        LinkedList<TreeNode> queue1 = new LinkedList<>();
        LinkedList<TreeNode> queue2 = new LinkedList<>();
        queue1.offer(root);
        queue2.offer(root);
        
        while(!queue1.isEmpty() && !queue2.isEmpty()){
            TreeNode node1 = queue1.poll();
            TreeNode node2 = queue2.poll();
            
            if(node1 == null && node2 == null)
                continue;
            else if(node1 == null || node2 == null)
                return false;
            else if(node1.val != node2.val)
                return false;
            
            queue1.offer(node1.left);
            queue1.offer(node1.right);
            
            queue2.offer(node2.right);
            queue2.offer(node2.left);
        }
        
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值