Symmetric Tree leetcode

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   /   2   2
 / \ / 3  4 4  3

But the following is not:

    1
   /   2   2
   \      3    3

看到这个题首先想到的是中序遍历,遍历结果是否是前后相同,也就是是否是“回文”,我使用了一个栈,遍历到root之前是push,遍历到root之后查看是否相同。中序遍历使用了非递归方法,代码写好后,运行测试发现有逻辑漏洞,如[1,2,3,3,#,2,#]这个树的中序遍历就是3,2,1,2,3也是回文,看来仅仅中序遍历是无法判断镜像树的。该思路错误

查看了下网上其它人的答案发现可以使用队列或栈来实现思路就是两两比较,左子树与右子树比较,左子树的左子树与右子树的右子树比较,左子树的右子树与右子树的左子树比较
bool isSymmetric(TreeNode* root) {
    if (!root) return true;
    queue<TreeNode*> check;

    check.push(root->left);
    check.push(root->right);

    while (!check.empty()) {
        TreeNode* node1 = check.front();
        check.pop();
        TreeNode* node2 = check.front();
        check.pop();
        if (!node1 && node2) return false;
        if (!node2 && node1) return false;
        if (node1 && node2) {
            if (node1->val != node2->val) return false;
            check.push(node1->left);
            check.push(node2->right);
            check.push(node1->right);
            check.push(node2->left);
        }
    }

    return true;
}
 
时间: 2024-08-03 17:47:27

Symmetric Tree leetcode的相关文章

Symmetric Tree leetcode java

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both

101. Symmetric Tree Leetcode Python

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both rec

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

LeetCode OJ - Symmetric Tree &amp;&amp; Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

[LeetCode][JavaScript]Symmetric Tree

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 https://leetcode.com/problems/symm

Leetcode 树 Symmetric Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Symmetric Tree Total Accepted: 13991 Total Submissions: 44240 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmet

[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 is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

LeetCode: Symmetric Tree [101]

[题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it bot

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *