Leetcode-101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
递归:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSymmetric(TreeNode* root) { bool flag = is(root, root); return flag; } bool is(TreeNode* r1, TreeNode* r2) { if(r1== NULL&& r2== NULL) return true; if(r1== NULL|| r2== NULL) return false; if(r1->val != r2->val) return false; bool flag1 = is(r1->left, r2->right); bool flag2 = is(r1->right, r2->left); if(! flag1|| ! flag2) return false; return true; } };