关于LeetCode中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.

    这道题就是我昨天在博客中提到了做了好久还没做出来的题目,题目的要求是判断一个二叉树是不是对称的,今天早上在到学校的公交车上想到了一种利用栈,在循环中进行push,pop然后根据具体节点的情况进行判断的方法,当然了这个方法的速度不是很快,而且还需要多开两个栈进行push和pop操作,空间复杂度也不小。但是解决了问题还是非常开心的,虽然我水平暂时有限,那么就先说一下这种方法解决问题的思路。总体思路就是判断根结点的两个子树是不是对称的,对称的要求是什么呢?首先对称的两个节点它们的val值应该是相同的,然后假如一个节点是其父节点的左子节点,那与它对称的节点一定是其对应父节点的右子节点。那在栈操作中的区别就是,对于栈stack1,假如先压入的是其左子节点,后压入的是其右子节点。那么,对于另一个栈stack2而言,就应该先压入其右子节点,然后压入其左子节点。这样在下次循环进行弹出的时候,stack1弹出的就是左子节点,而stack2弹出的是其右子节点,这样就可以直接进行值判断从而得到这两个节点是不是对称的。以此类推,将所有将所有树中的节点都过一遍后,若程序没有返回false,则说明这个二叉树是对称的,直接返回true即可。然后是已Accepted的代码:

    public boolean isSymmetric(TreeNode root) {
        if(root==null || (root.right==null && root.left==null)){
            return true;
        }else if(root.right!=null && root.left!=null){
            Stack<TreeNode> stack1 = new Stack<TreeNode>();
            Stack<TreeNode> stack2 = new Stack<TreeNode>();
            stack1.push(root.left);
            stack2.push(root.right);
            while(!stack1.empty() && !stack2.empty()){
                TreeNode temp1 = stack1.pop();
                TreeNode temp2 = stack2.pop();
                if(temp1.val!=temp2.val){
                    return false;
                }
                if(temp1.right!=null&&temp1.left!=null&&temp2.right!=null&&temp2.left!=null){
                    stack1.push(temp1.left);
                    stack1.push(temp1.right);
                    stack2.push(temp2.right);
                    stack2.push(temp2.left);
                }else if(temp1.right==null&&temp1.left!=null&&temp2.right!=null&&temp2.left==null){
                    stack1.push(temp1.left);
                    stack2.push(temp2.right);
                }else if(temp1.right!=null&&temp1.left==null&&temp2.right==null&&temp2.left!=null){
                    stack1.push(temp1.right);
                    stack2.push(temp2.left);
                }else if(temp1.right==null&&temp1.left==null&&temp2.right==null&&temp2.left==null){
                    
                }else{
                    return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }
    这个题目是有Editoral Solution的,所以还是比较有意思的。Editoral Solution中介绍了一种递归的方法,一种迭代的方法,都是我之前没想到的,现在都贴到下面,首先是递归的方法:

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);
}
    然后是迭代的方法,用的数据结构是队列,代码如下:

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;
}
    最后放上这道题Editoral Solution的地址,里面有问题解析和时间复杂度分析,地址如下: https://leetcode.com/articles/symmetric-tree/

    今天真是累死宝宝了,又跑了一天去办各种杂事,一想到明天还得6点多起来,感觉身体被掏空。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值