Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / 2 2/\/3 4 4 3
But the following are 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.
/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public: bool Issymmetric (TreeNode *root) { if (root = NULL) return true; Return Issymmetric (root->left,root->right); } BOOL Issymmetric (treenode* left,treenode* right) { if (left==null && right==null) return true; if (left==null | | right==null) return false; bool L = issymmetric (left->right,right->left); if (left->val! = Right->val) return false; BOOL R = issymmetric (left->left,right->right); Return l&&r; }};
Leetcode--symmetric Tree