LeetCode - Symmetric Tree
阿新 • • 發佈:2018-11-08
/** * 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) { if(!root) return true; return helper(root->right, root->left); } bool helper(TreeNode* left, TreeNode* right){ if(!left && !right) return true; if(!left || !right || left->val!=right->val) return false; return helper(left->left, right->right)&&helper(left->right, right->left); } };