101. 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 recursively and iteratively.

1. 递归

bool isSymmetric(TreeNode *p, TreeNode *q){
    if (p==NULL && q==NULL) return true;
    if (p==NULL || q==NULL) return false;

    return (p->val == q->val) &&
            isSymmetric(p->left, q->right) &&
            isSymmetric(p->right, q->left);
}

2. 非递归

bool isSymmetric(TreeNode *p, TreeNode *q)
{
    queue<TreeNode*> q1;
    queue<TreeNode*> q2;
    q1.push(p);
    q2.push(q);
    while(q1.size()>0 && q2.size()>0){
        TreeNode* p1 = q1.front();
        q1.pop();
        TreeNode* p2 = q2.front();
        q2.pop();
        if (p1==NULL && p2==NULL) continue;
        if (p1==NULL || p2==NULL) return false;

        if (p1->val != p2->val) return false;

        q1.push(p1->left);
        q2.push(p2->right);

        q1.push(p1->right);
        q2.push(p2->left);

    }
    return true;
}
时间: 2024-08-29 04:38:59

101. Symmetric Tree -- 判断树结构是否对称的相关文章

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 判断是否为对称树(AC代码)

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode *root) {

101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的.    1   / \  2   2 / \ / \3  4 4  3但是下面这个 [1,2,2,null,3,null,3] 则不是:    1   / \  2   2   \   \   3    3说明:如果你可以递归地和迭代地解决它就奖励你点数.详见:https://leetcode.com/problems/symmetric-tree/description

leetCode 101. Symmetric Tree 对称树

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

Leetcode: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 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

&amp;lt;LeetCode OJ&amp;gt; 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficulty: Easy 给定一颗二叉树,检查是否镜像对称(环绕中心对称) Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this bin

LeetCode 101 Symmetric Tree (C)

题目: 101. Symmetric Tree QuestionEditorial Solution My Submissions Total Accepted: 135232 Total Submissions: 375037 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【61】101. Symmetric Tree

101. Symmetric Tree Description Submission Solutions Add to List Total Accepted: 154374 Total Submissions: 414598 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【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; *