🔥Easy Solutions in Java 📝, Python 🐍, and C++ 🖥️🧐Look at once 💻
Intuition\n Describe your first thoughts on how to solve this problem. \n To check if a binary tree is symmetric, we need to compare its left subtree and right 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n To check if a binary tree is symmetric, we need to compare its left subtree and right subtree. To do this, we can traverse the tree recursively and compare the left and right subtrees at each level. If they are symmetric, we continue the traversal. Otherwise, we can immediately return false.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can define a recursive helper function that takes two nodes as input, one from the left subtree and one from the right subtree. The helper function returns true if both nodes are null, or if their values are equal and their subtrees are symmetric.\n\n\n# Complexity\n- Time complexity:The time complexity of the algorithm is $$O(n)$$, where n is the number of nodes in the binary tree. We need to visit each node once to check if the tree is symmetric.\n- Space complexity:\nThe space complexity of the algorithm is $$O(h)$$, where h is the height of the binary tree. In the worst case, the tree can be completely unbalanced, and the recursion stack can go as deep as the height of the tree.\n\n\n![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n# Code\n``` Java []\nclass Solution {\n    public boolean isSymmetric(TreeNode root) {\n        if (root == null) {\n            return true;\n        }\n        return isMirror(root.left, root.right);\n    }\n    \n    private boolean isMirror(TreeNode node1, TreeNode node2) {\n        if (node1 == null && node2 == null) {\n            return true;\n        }\n        if (node1 == null || node2 == null) {\n            return false;\n        }\n        return node1.val == node2.val && isMirror(node1.left, node2.right) && isMirror(node1.right, node2.left);\n    }\n}\n\n```\n```Python []\nclass Solution(object):\n    def isMirror(self, left, right):\n        if not left and not right:\n            return True\n        if not left or not right:\n            return False\n        return left.val == right.val and self.isMirror(left.left, right.right) and self.isMirror(left.right, right.left)\n    \n    def isSymmetric(self, root):\n        if not root:\n            return True\n        return self.isMirror(root.left, root.right)\n\n```\n```C++ []\nclass Solution {\npublic:\n    bool isMirror(TreeNode* left, TreeNode* right) {\n    if (!left && !right) return true;\n    if (!left || !right) return false;\n    return (left->val == right->val) && isMirror(left->left, right->right) && isMirror(left->right, right->left);\n}\n\nbool isSymmetric(TreeNode* root) {\n    if (!root) return true;\n    return isMirror(root->left, root->right);\n}\n\n};\n\n\n```\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```
['Depth-First Search', 'Binary Tree', 'Python', 'C++', 'Java']