资讯专栏INFORMATION COLUMN

leetcode101. Symmetric Tree

IntMain / 1193人阅读

摘要:题目要求检查一棵树是否是左右对称的。递归在这里递归的一般情况是,输入进行比较的左子树和右子树的根节点,先判断该俩根节点是否等价,然后判断子节点是否等价。栈通过栈的形式同样可以实现比较。将需要进行比较的节点依次压入栈中。

题目要求
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.

检查一棵树是否是左右对称的。

递归

在这里递归的一般情况是,输入进行比较的左子树和右子树的根节点,先判断该俩根节点是否等价,然后判断子节点是否等价。

    public boolean isSymmetric(TreeNode treeNode){
        if(treeNode==null) return true;
        return isSymmetric(treeNode.left, treeNode.right);
    }
    
    public boolean isSymmetric(TreeNode left, TreeNode right){
        if(left==null && right==null) return true;
        if(left!=null && right!=null && left.val==right.val){
            return isSymmetric(left.left, right.right)&&isSymmetric(left.right, right.left);
        }
        return false;
    }

通过栈的形式同样可以实现比较。将需要进行比较的节点依次压入栈中。每次从栈中取出两个进行比较的节点比较。有点像level traversal的思路。

    public boolean isSymmetric2(TreeNode treeNode){
        if(treeNode==null) return true;
        LinkedList stack = new LinkedList();
        TreeNode left = treeNode.left, right = treeNode.right;
        stack.push(left);
        stack.push(right);
        while(!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            if(right==null && left==null)continue;
            else if(left==null || right==null)return false;
            else if(left.val==right.val){
                stack.push(left.left);
                stack.push(right.right);
                stack.push(left.right);
                stack.push(right.left);
            }else{
                return false;
            }
        }
        return true;
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67455.html

相关文章

  • leetcode-101-Symmetric Tree-二叉树对称问题

    摘要:解题思路所谓的对称,是左右相反位置的节点的值判断是否相同。只要出现不同,即可返回即可,否则继续进行处理。 topic: 101. Symmetric Tree Description: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For...

    seanlook 评论0 收藏0
  • leetcode部分题目答案之JavaScript版

    摘要:自己没事刷的一些的题目,若有更好的解法,希望能够一起探讨项目地址 自己没事刷的一些LeetCode的题目,若有更好的解法,希望能够一起探讨 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 评论0 收藏0
  • [LeetCode] Symmetric Tree

    Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Example For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / / 3 ...

    wfc_666 评论0 收藏0
  • [Leetcode] Same Tree Symmetric Tree 相同树 对称树

    摘要:递归法复杂度时间空间递归栈空间思路如果两个根节点一个为空,一个不为空,或者两个根节点值不同,说明出现了不一样的地方,返回假。代码递归法复杂度时间空间递归栈空间思路其实和写法是一样的,是比较两个节点的两个左边,然后再比较两个节点的两个右边。 Same Tree Given two binary trees, write a function to check if they are eq...

    TZLLOG 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月上半月汇总(55 题攻略)

    摘要:微信公众号记录截图记录截图目前关于这块算法与数据结构的安排前。已攻略返回目录目前已攻略篇文章。会根据题解以及留言内容,进行补充,并添加上提供题解的小伙伴的昵称和地址。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目录 不...

    warmcheng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<