"Sword refers to offer" symmetric binary tree, "Sword refers to offer"
[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]
Question link: http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb? Rp = 3 & ru =/ta/coding-interviews & qru =/ta/coding-interviews/question-ranking
Description
Implement a function to determine whether a binary tree is symmetric. Note: If a binary tree is the same as the image of this binary tree, it is defined as symmetric.
Ideas
Judge the symmetry of its Subtrees recursively from the root node
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution{public:bool isSymmetrical(TreeNode* pRoot){return check(pRoot,pRoot);}bool check(TreeNode *pLeft,TreeNode *pRight){if(pLeft==nullptr && pRight==nullptr)return true;if(pLeft==nullptr || pRight==nullptr)return false;if(pLeft->val != pRight->val)return false;return check(pLeft->left,pRight->right)&&check(pLeft->right,pRight->left);}};
Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source