题目描述:
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ #include<stack> class Solution { public: bool isSymmetrical(TreeNode* pRoot) { if(pRoot == NULL) return true; stack<TreeNode *> s ; s.push(pRoot->left); s.push(pRoot->right); while(!s.empty()) { TreeNode *right = s.top();//成对取出 s.pop(); TreeNode *left = s.top(); s.pop(); if(left == NULL && right == NULL) continue; if(left == NULL || right == NULL) return false; if(left->val != right->val) return false; //成对插入 s.push(left->left); s.push(right->right); s.push(left->right); s.push(right->left); } return true; } };