题解 | #特殊乘法#
判断二叉树是否对称
http://www.nowcoder.com/practice/1b0b7f371eae4204bc4a7570c84c2de1
利用DFS和镜像DFS进行先序遍历,遇到空则记录'#' 最后判断两个遍历字符串是否相等
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
string pre="";
string pre_="";
class Solution {
public:
/**
*
* @param root TreeNode类
* @return bool布尔型
*/
void DFS(TreeNode* root)
{
if(root==NULL)
{
pre+="#";
return;
}
pre+=to_string(root->val);
DFS(root->left);
DFS(root->right);
}
void DFS_(TreeNode* root)
{
if(root==NULL)
{
pre_+="#";
return;
}
pre_+=to_string(root->val);
DFS_(root->right);
DFS_(root->left);
}
bool isSymmetric(TreeNode* root) {
// write code here
DFS(root);
DFS_(root);
cout<<pre<<endl;
cout<<pre_<<endl;
if(pre==pre_)
return true;
else
return false;
}
};