# "Symmetric Tree" and "Remove Element"

Symmetric Tree:

//一开始用了中序遍历树，检查输出数组是否对称的方法，上传代码后juage错误。
class Solution {
public:
vector<int> temp;
bool isSymmetric(TreeNode* root) {
if (root == NULL)
return true;
middle_order(root);
auto _1_it = temp.begin();
auto _2_it = temp.end() - 1;

for (; _2_it > _1_it; _1_it++, _2_it--)
{
if (*_1_it != *_2_it)
return false;
}
return true;
}
void middle_order(TreeNode* root)
{
if (root != NULL)
{
middle_order(root->left);
temp.push_back(root->val);
middle_order(root->right);
}
}
};

class Solution {
public:
bool isSymmetric(TreeNode* root)
{

if (root == NULL) return true;

return ifSymmetric(root->left, root->right);
}

bool ifSymmetric(TreeNode* tree1, TreeNode* tree2)
{
if (tree1 == NULL && tree2 == NULL)
return true;
if (tree1 == NULL || tree2 == NULL)
return false;

if (tree1->val != tree2->val)
return false;
else
return (ifSymmetric(tree1->left, tree2->right) && ifSymmetric(tree1->right, tree2->left));
}
};

Remove Element:

class Solution {
public:
int removeElement(vector<int>& nums, int val) {

for (auto it = nums.begin(); it != nums.end();)
{
if (*it == val)
it = nums.erase(it);
else
it++;
}
return nums.size();
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章："Symmetric Tree" and "Remove Element" 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)