资讯专栏INFORMATION COLUMN

[LeetCode] Symmetric Tree

wfc_666 / 2519人阅读

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  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.

Solution Recursion
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return dfs(root.left, root.right);
    }
    private boolean dfs(TreeNode left, TreeNode right) {
        if (left == null) return right == null;
        if (right == null) return left == null;
        if (left.val != right.val) {
            return false;
        }
        return dfs(left.left, right.right) && dfs(left.right, right.left);
    }
}
Iteration
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        Queue queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()) {
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();
            if (left == null ^ right == null) return false;
            if (left == null && right == null) continue;
            if (left.val != right.val) return false;
            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }
        return true;
    }
}

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

转载请注明本文地址:https://www.ucloud.cn/yun/69270.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
  • leetcode101. Symmetric Tree

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

    IntMain 评论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部分题目答案之JavaScript版

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

    alphahans 评论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条评论

wfc_666

|高级讲师

TA的文章

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