Symmetric Tree

来源:互联网 发布:软件测试知识点 编辑:程序博客网 时间:2024/06/14 02:06

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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


递归解法:

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {private:bool isSymm(TreeNode *leftNode, TreeNode *rightNode){      if((leftNode==NULL) && (rightNode==NULL))      {          return true;      }                  if((leftNode!=NULL) && (rightNode==NULL))      {          return false;      }                          if((leftNode==NULL) && (rightNode!=NULL))      {          return false;      }                  if((leftNode!=NULL) && (rightNode!=NULL))      {          return (leftNode->val==rightNode->val) && isSymm(leftNode->left, rightNode->right) && isSymm(leftNode->right, rightNode->left);      }        }public:    bool isSymmetric(TreeNode *root) {        if(root==NULL)            return true;            return isSymm(root->left, root->right);            }};


非递归解法:

开始思路是广度优先遍历tree,每层的点全部存入一个队列,然后判断这个队列是否对称。在判断队列是否对称的时候,用vector把队列里面的值存了一遍又,虽然能作对,但是报错是内存超出最大限制。

后来经某同学提示,改成用两个queue分别存储左子树和右子树,但存右子树的时候先存右节点再存左节点,顺利搞定:)

<pre name="code" class="cpp">/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isSymmetric(TreeNode *root) {        if(root==NULL)            return true;                TreeNode *leftNode = root->left;        TreeNode *rightNode = root->right;                if((leftNode==NULL) && (rightNode==NULL))        {             return true;        }        else if((leftNode==NULL) || (rightNode==NULL))        {          return false;        }              if(leftNode->val != rightNode->val)               {          return false;        }                queue<TreeNode *> stackLeft;        queue<TreeNode *> stackRight;              stackLeft.push(leftNode);        stackRight.push(rightNode);                while(!stackLeft.empty())        {                      TreeNode *tmp1 = stackLeft.front();            stackLeft.pop();                        TreeNode *tmp2 = stackRight.front();            stackRight.pop();                    if(tmp1!=NULL && tmp2!=NULL)            {                if(tmp1->val!=tmp2->val)                {                    return false;                }            }            else if((tmp1==NULL && tmp2!=NULL) || (tmp1!=NULL && tmp2==NULL))            {                return false;            }            if(tmp1==NULL && tmp2==NULL){continue;}            stackLeft.push(tmp1->left);            stackLeft.push(tmp1->right);                        stackRight.push(tmp2->right);            stackRight.push(tmp2->left);        }                return stackLeft.empty() && stackRight.empty();                    }};


                                             
0 0
原创粉丝点击