Symmetric Tree

原创 2015年11月20日 16:03:29
  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);
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

判断二叉树是否是水平对称的 Symmetric Tree

题目源自于Leetcode。 题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...

Symmetric Tree(中心对称树)

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

101:Symmetric Tree【树】【DFS】

题目链接:click~ :解法1:队列 /*题意:判断一颗二叉树是否是自己的镜像*/ /** *思路: 只要保证根结点下的左子树的每一层是右子树的镜像,此树是自己的镜像 * ...

[leet code] Symmetric Tree

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

【LeetCode】Symmetric Tree

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

leetcode || 101、Symmetric Tree

problem: Given a binary tree, check whether it is a mirror of itself (ie, symmetric aro...

【LeetCode】Symmetric Tree

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

Symmetric Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/symmetric-tree/  这道题是树的题目,本质上还是树的遍历。这里无所谓哪种遍历方式,只需要对相应结点进行比较即可...

Symmetric Tree(对称树)

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

LeetCode#101. Symmetric Tree

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Symmetric Tree
举报原因:
原因补充:

(最多只允许输入30个字)