leetcode || 101、Symmetric Tree

problem:

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.

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

Hide Tags

Tree Depth-first
Search

题意:判断一棵二叉树是否为镜像二叉树(结构对称,数字相等)

thinking:

(1)该题合适的解法是递归法,采用DFS思想

递归,保存左右两个节点,然后判断leftNode->left和rightNode->right,以及leftNode->right和rightNode->left。如此不断递归

(2)还想到另外一种非递归法:层序遍历法,子结点为NULL时代表数值0,从左往右层序遍历得到数组a,从右往左层序遍历得到数组b

如果a==b,则该二叉树是镜像的。但提交时显示:Status:

Memory Limit Exceeded

code:

递归法:

class Solution {
public:
    bool check(TreeNode *leftNode, TreeNode *rightNode)
    {
        if (leftNode == NULL && rightNode == NULL)
            return true;

        if (leftNode == NULL || rightNode == NULL)
            return false;

        return leftNode->val == rightNode->val && check(leftNode->left, rightNode->right) &&
            check(leftNode->right, rightNode->left);
    }

    bool isSymmetric(TreeNode *root) {
        if (root == NULL)
            return true;

        return check(root->left, root->right);
    }
};

层序遍历法:Status:

Memory Limit Exceeded

class Solution {
private:
    vector<int> res1;
    vector<int> res2;
public:
    bool isSymmetric(TreeNode *root) {
        if(root==NULL)
            return true;
        res1.clear();
        res2.clear();
        level_walk_left(root);
        level_walk_right(root);
        if(res1==res2)
            return true;
        else
            return false;
    }
protected:
    void level_walk_left(TreeNode *root)
    {
        queue<TreeNode *> _queue;
        _queue.push(root);
        while(!_queue.empty())
        {
            TreeNode *tmp=_queue.front();
            if(tmp==NULL)
                res1.push_back(0);
            else
            {
                res1.push_back(tmp->val);
                _queue.pop();
                if(tmp->left!=NULL)
                    _queue.push(tmp->left);
                else
                    _queue.push(NULL);
                if(tmp->right!=NULL)
                    _queue.push(tmp->right);
                else
                    _queue.push(NULL);
            }
        }
    }

    void level_walk_right(TreeNode *root)
    {
        queue<TreeNode *> _queue;
        _queue.push(root);
        while(!_queue.empty())
        {
            TreeNode *tmp=_queue.front();
            if(tmp==NULL)
                res2.push_back(0);
            else
            {
                res2.push_back(tmp->val);
                _queue.pop();
                if(tmp->right!=NULL)
                    _queue.push(tmp->right);
                else
                    _queue.push(NULL);
                if(tmp->left!=NULL)
                    _queue.push(tmp->left);
                else
                    _queue.push(NULL);
            }
        }
    }

};
时间: 2024-08-29 22:00:21

leetcode || 101、Symmetric Tree的相关文章

【LeetCode OJ 101】Symmetric Tree

题目链接:https://leetcode.com/problems/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 \

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

LeetCode OJ:Symmetric Tree 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 判断一颗树是否对称,首先用递归的方法当然比较容易解决: 1 /** 2 * Definition

leetcode || 100、Same Tree

problem: 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. Hide Tags Tree Depth-first Search 题意:检查两棵 二叉树是否相同 ,二叉树相同定

leetcode || 144、Binary Tree Preorder Traversal

problem: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归前序遍历二叉树 t

leetcode || 103、Binary Tree Zigzag Level Order Traversal

problem: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 retur

leetcode || 94、Binary Tree Inorder Traversal

problem: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means?

leetcode || 107、Binary Tree Level Order Traversal II

problem: 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 or

leetcode || 102、Binary Tree Level Order Traversal

problem: 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