101. Symmetric Tree (Tree, Queue; DFS, WFS)

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.

思路:要等到左儿子和右儿子的结果都知道了,才能判断当前节点的对称性,所以是后序遍历

法I:递归后序遍历

class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if(!root) return true;

        bool result;
        if((root->left==NULL && root->right != NULL) ||(root->left!=NULL && root->right == NULL))
        {
            return false;
        }
        else if(root->left == NULL && root->right == NULL)
        {
            return true;
        }
        else
        {
            result = cmp(root->left, root->right);
        }
    }
    bool cmp(TreeNode * node1, TreeNode* node2)
    {
        int result1 = true;
        int result2 = true;
        if(node1->val!=node2->val) return false;
        else
        {
            //递归结束条件:至少有一个节点为NULL
            if((node1->left==NULL && node2->right != NULL) ||
            (node1->left!=NULL && node2->right == NULL)||
            (node1->right!=NULL && node2->left == NULL)||
            (node1->right==NULL && node2->left != NULL))
            {
                return false;
            }
            if((node1->left == NULL && node2->right == NULL)&&
            (node1->right == NULL && node2->left== NULL))
            {
                return true;
            }

            //互相比较的两个点,要比较节点1的左儿子和节点2的右儿子,以及节点1的右儿子和节点2的左儿子
            if(node1->left != NULL)
            {
                result1 = cmp(node1->left,node2->right);
            }
            if(node1->right != NULL)
            {
                result2 = cmp(node1->right,node2->left);
            }
            return (result1 && result2);
        }
    }
};

法II:用队列实现层次遍历(层次遍历总是用队列来实现)

class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if(root == NULL)    return true;
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        TreeNode *t1, *t2;
        while(!q.empty()){
            t1 = q.front();
            q.pop();
            t2 = q.front();
            q.pop();
            if(t1 == NULL && t2 == NULL)
                continue;
            if(t1 == NULL || t2 == NULL || t1->val != t2->val)
                return false;
            q.push(t1->left);
            q.push(t2->right);
            q.push(t1->right);
            q.push(t2->left);
        }
        return true;
    }
};
时间: 2024-08-08 11:13:06

101. Symmetric Tree (Tree, Queue; DFS, WFS)的相关文章

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] is not: 1 / 2 2 \ 3 3 Note: Bonus

&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

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

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详细分析 :: Recover Binary Search Tree [Tree]

Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

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 recu

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] is not: 1 / 2 2 \ 3 3 Note:Bonus

101. Symmetric Tree - Easy

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