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,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

思路:朴素思想是基于层次遍历,然后判断每一层是否对称,然后再判断下一层。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> layer = new LinkedList<TreeNode>();  
        ArrayList<TreeNode> compara = new ArrayList<TreeNode>();
        layer.add(root);
        while(!layer.isEmpty()){          
            while(!layer.isEmpty()){
                TreeNode nowNode = layer.poll();
                if(nowNode == null) continue;
                compara.add(nowNode.left);
                compara.add(nowNode.right);
            }
            for(int left = 0,right = compara.size() - 1;left < right; left++,right--){
                if(compara.get(left) != null && compara.get(right) != null &&compara.get(left).val ==  compara.get(right).val){
                    continue;
                }else if(compara.get(left) == null && compara.get(right) == null){
                    continue;
                }else{
                    System.out.println();
                    return false;
                }
            }
            layer.addAll(compara);  
            compara.clear();
        }
        return true;
    }
}

当然,还有改进写法,可以减少一个辅助数组。

public boolean isSymmetric(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    q.add(root);
    while (!q.isEmpty()) {
        TreeNode t1 = q.poll();
        TreeNode t2 = q.poll();
        if (t1 == null && t2 == null) continue;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        q.add(t1.left);
        q.add(t2.right);
        q.add(t1.right);
        q.add(t2.left);
    }
    return true;
}

public boolean isSymmetric(TreeNode root) {
    return isMirror(root, root);
}

public boolean isMirror(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return (t1.val == t2.val)
        && isMirror(t1.right, t2.left)
        && isMirror(t1.left, t2.right);
}
时空复杂度都是O(N),因为遍历所有节点,且使用了N的辅助空间(队列或者递归栈)。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用C#语言生成一棵树(二叉链表)的示例代码,并且实现判断该树是否对称叉树的功能: ``` using System; class Node { public int data; public Node left, right; public Node(int value) { data = value; left = right = null; } } class BinaryTree { Node root; public BinaryTree() { root = null; } private bool isSymmetric(Node left, Node right) { if (left == null && right == null) return true; if (left == null || right == null) return false; return (left.data == right.data) && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); } public bool isSymmetric() { if (root == null) return true; return isSymmetric(root.left, root.right); } static void Main(string[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(2); tree.root.left.left = new Node(3); tree.root.left.right = new Node(4); tree.root.right.left = new Node(4); tree.root.right.right = new Node(3); if (tree.isSymmetric()) Console.WriteLine("The tree is symmetric"); else Console.WriteLine("The tree is not symmetric"); } } ``` 这段代码中,我们首先定义了一个节点类`Node`,其中包含了节点的值、左子节点和右子节点。接着,我们定义了一个二叉树类`BinaryTree`,其中包含了二叉树的根节点和判断叉树是否对称的核心函数`isSymmetric`。这个函数采用递归的方式,判断左右子树是否对称同构。 在`Main`函数中,我们创建了一棵树,并调用了`isSymmetric`函数来判断该树是否对称叉树。如果是,则输出"The tree is symmetric";否则,输出"The tree is not symmetric"。 希望这段代码能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值