【LeetCode】Symmetric Tree (2 solutions)

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、left对应right

2、left->left对应right->right

3、left->right对应right->left

解法一:递归

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if(root == NULL)
            return true;
        else
            return Helper(root->left, root->right);
    }
    bool Helper(TreeNode* left, TreeNode* right)
    {
        if(!left && !right)
            return true;
        else if(!left && right)
            return false;
        else if(left && !right)
            return false;
        else
        {
            if(left->val != right->val)
                return false;
            else
            {//recursion
                bool partRes1 = Helper(left->left, right->right);
                bool partRes2 = Helper(left->right, right->left);
                return partRes1 && partRes2;
            }
        }
    }
};

解法二:非递归

使用两个队列,对左右子树分别进行层次遍历。

进队时的对应元素比较即可。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if(root == NULL)
            return true;
        else if(root->left && !root->right)
            return false;   //right is NULL
        else if(!root->left && root->right)
            return false;   //left is NULL
        else if(!root->left && !root->right)
            return true;    //both NULL
        else
        {
            if(root->left->val != root->right->val)
                return false;
            else
            {
                queue<TreeNode*> lq;
                queue<TreeNode*> rq;
                lq.push(root->left);
                rq.push(root->right);
                while(!lq.empty() && !rq.empty())
                {
                    TreeNode* lfront = lq.front();
                    lq.pop();
                    TreeNode* rfront = rq.front();
                    rq.pop();
                    //lfront->left vs. rfront->right
                    if(lfront->left && !rfront->right)
                        return false;   //rfront->right is NULL
                    else if(!lfront->left && rfront->right)
                        return false;   //lfront->left is NULL
                    else if(!lfront->left && !rfront->right)
                        ;   //both NULL
                    else
                    {
                        if(lfront->left->val != rfront->right->val)
                            return false;
                        else
                        {
                            lq.push(lfront->left);
                            rq.push(rfront->right);
                        }
                    }
                    //lfront->right vs. rfront->left
                    if(lfront->right && !rfront->left)
                        return false;   //rfront->left is NULL
                    else if(!lfront->right && rfront->left)
                        return false;   //lfront->right is NULL
                    else if(!lfront->right && !rfront->left)
                        ;   //both NULL
                    else
                    {
                        if(lfront->right->val != rfront->left->val)
                            return false;
                        else
                        {
                            lq.push(lfront->right);
                            rq.push(rfront->left);
                        }
                    }
                }
                return true;
            }
        }
    }
};

时间: 2024-08-03 21:39:20

【LeetCode】Symmetric Tree (2 solutions)的相关文章

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

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【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 Note:Bonus points if you could sol

【LeetCode】Same Tree (2 solutions)

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解法一:递归 /** * Definition for binary tree * struct TreeNod

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread