> For the complete documentation index, see [llms.txt](https://anton-veselskyi.gitbook.io/codding-problems-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anton-veselskyi.gitbook.io/codding-problems-solutions/leetcode/easy/symmetric-tree.md).

# Symmetric Tree

## [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 `[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
```

**Follow up:** Solve it both recursively and iteratively.

## Solutions

### 🧠 Cpp

```cpp
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

#include <utility>
class Solution
{
public:
    bool isSymmetric(TreeNode* root)
    {
        if(!root)
            return true;
        if( !!root->left != !!root->right)
            return false;
        if(!root->left)
            return true;

        //idea use level-traversal 
        //to check that left and right side has symetric leafs

        //we will check level by level
        //each level will have 2*n elements
        list<TreeNode*> level{root->left, root->right};

        while(!level.empty())
        {
            list<TreeNode*> next_level_left;
            list<TreeNode*> next_level_right;

            for(TreeNode *left_el=level.front(), *right_el =level.back();
                !level.empty();
                left_el=level.front(), right_el = level.back())
            {
                //check for symmetric form
                if( !!left_el->left == !!right_el->right && !!left_el->right == !!right_el->left
                //check for same content
                   && left_el->val == right_el->val )
                {
                    if(left_el->left)
                    {
                        next_level_left.emplace_back(left_el->left);
                        next_level_right.emplace_back(right_el->right);
                    }
                    if(left_el->right)
                    {
                        next_level_left.emplace_back(left_el->right);
                        next_level_right.emplace_back(right_el->left);
                    }

                    level.pop_front();
                    level.pop_back();
                }
                else
                    return false;
            }

            reverse(next_level_right.begin(), next_level_right.end());
            level = std::move(next_level_left);
            level.insert(level.end(),
                         std::make_move_iterator(next_level_right.begin()),
                         std::make_move_iterator(next_level_right.end()));


        }

        return true;


    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anton-veselskyi.gitbook.io/codding-problems-solutions/leetcode/easy/symmetric-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
