标签:std algo problem root tree null solution stream 个性
/** 对称二叉树:
* 判断一颗二叉树是否是对称二叉树
**/
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
1
/ 2 2
\ 3 3
1
/ 2 2
/ \ / 3 4 4 3
**/
class Solution {
public:
bool isMirro(TreeNode* tree1,TreeNode* tree2){
if(tree1==NULL&&tree2==NULL){
return true;
}
if(!tree1||!tree2)
return false;
if(tree1->val==tree2->val){
return isMirro(tree1->left,tree2->right)&&isMirro(tree1->right,tree2->left);
}else{
return false;
}
}
bool isSymmetric(TreeNode* root) {
if(root)
return isMirro(root->left,root->right);
else return true;
}
};
int main(){
TreeNode* t1=new TreeNode(1);
TreeNode* t2=new TreeNode(2);
TreeNode* t3=new TreeNode(2);
TreeNode* t4=new TreeNode(3);
TreeNode* t5=new TreeNode(4);
TreeNode* t6=new TreeNode(4);
TreeNode* t7=new TreeNode(3);
t2->left=t4;t2->right=t5;
t3->left=t6;t3->right=t7;
t1->left=t2;t1->right=t3;
Solution solution;
solution.isSymmetric(t1);
system("pause");
return 0;
}
标签:std algo problem root tree null solution stream 个性
原文地址:https://www.cnblogs.com/GarrettWale/p/12343277.html