面试题28. 对称的二叉树

题目:

面试题28. 对称的二叉树

 

 

解答:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode* root) 
13     {
14         if (NULL == root)
15         {
16             return true;
17         }
18 
19         return helper(root->left, root->right);
20 
21     }
22 
23     bool helper(TreeNode *a, TreeNode *b)
24     {
25         if (a == NULL && b == NULL)
26         {
27             return true;
28         }
29         
30         if (a == NULL || b == NULL)
31         {
32             return false;
33         }
34         
35         if (a->val != b->val)
36         {
37             return false;
38         }
39         
40         return helper(a->left, b->right) && helper(a->right, b->left);
41     }
42 
43 };

 

上一篇:Chrome浏览器中,xpath_helper插件的安装方法和使用方式。(内附安装包地址)


下一篇:vue学习-10-表单输入绑定