101. Symmetric Tree

1. 问题描述

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

   1
  / \
  2   2
 / \  / \
 3  4 4   3
But the following [1,2,2,null,3,null,3] is not:
 1
/ \
2  2
 \   \
  3   3

Note:

Bonus points if you could solve it both recursively and iteratively.
Tags: Tree Depth-first Search Breadth-first Search

2. 解题思路
3. 代码

class Solution {
public:
    bool isSymmetric(TreeNode* root) 
    {
        if (!root)
        {
            return true;
        }
        if (NULL != root->left && NULL != root->right)
        {
            return isSymme(root->left, root->right);
        }
        else if (NULL == root->left && NULL == root->right)
        {
            return true;
        }
        else
        {
            return false;    
        }
    }
private:
    bool isSymme(TreeNode* lh, TreeNode* rh)
    {
        if (NULL == lh && NULL == rh)
        {
            return true;
        }
        else if (NULL != lh && NULL != rh)
        {
            if (lh->val != rh->val)
            {
                return false;
            }
            else
            {
                return isSymme(lh->left, rh->right) && isSymme(lh->right, rh->left);
            }
        }
        else
        {
            return false;
        }        
    }
};

4. 反思

posted on 2016-08-20 23:43  whl-hl  阅读(160)  评论(0编辑  收藏  举报

导航