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 is symmetric:

        1
       / \
      2   2
     / \ / \
    3  4 4  3
    

    But the following is not:

        1
       / \
      2   2
       \   \
       3    3
    
  2. 解答
    bool isSymtotree(struct TreeNode *root1,struct TreeNode *root2)
    {
        if(root1==NULL && root2==NULL) return true;
         if(root1==NULL && root2!=NULL) return false;
        if(root1!=NULL && root2==NULL) return false;
        
        if(root1->val==root2->val)
        {
            return isSymtotree(root1->left,root2->right)&&isSymtotree(root1->right,root2->left);
        }
        return false;
    }
    bool isSymmetric(struct TreeNode* root)
    {
        if(root==NULL) return true;
        //struct TreeNode *root1=invertTree(root);
        //return isSameTree(root,root1);
        return isSymtotree(root->left,root->right);
    }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012201343/article/details/49947945
文章标签: 二叉树
个人分类: LeetCode
上一篇Unique Binary Search Trees II
下一篇Populating Next Right Pointers in Each Node
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭