码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 101. Symmetric Tree

时间:2015-02-17 17:37:13      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   /   2   2
 / \ / 3  4 4  3

 

But the following is not:

    1
   /   2   2
   \      3    3

 

Note:
Bonus points if you could solve it both recursively and iteratively.

[Solution]

递归判断

symCheck(left->left, right->right) && symCheck(left->right, right->left)
 1 bool isSymmetric(TreeNode *root) 
 2     {
 3         if (root == NULL)
 4             return true;
 5         return symCheck(root->left, root->right);
 6     }
 7     
 8     bool symCheck(TreeNode *left, TreeNode *right)
 9     {
10         if (left == NULL || right == NULL)
11         {
12             if (left == right)
13                 return true;
14             else
15                 return fasle;
16         }
17         
18         if (left->val == right->val)
19             return symCheck(left->left, right->right) && symCheck(left->right, right->left);
20         else
21             return false;
22     }

 

leetcode 101. Symmetric Tree

标签:

原文地址:http://www.cnblogs.com/ym65536/p/4295360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!