• [LeetCode]Symmetric Tree


    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.


    OJ's Binary Tree Serialization:

    The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

    Here's an example:

       1
      / 
     2   3
        /
       4
        
         5
    
    The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

    思考:

    递归:DFS,两个指针p、q。若p移到左子树,则q移到右子树,然后再比较。

    /**
     * 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 DFS(TreeNode *p,TreeNode *q)
        {
            if(!p&&!q) return true;
            else if(p&&q)
            {
                if(p->val!=q->val) return false;
                else return DFS(p->left,q->right)&&DFS(p->right,q->left);
            }
            else return false;
        }
        bool isSymmetric(TreeNode *root) {
            TreeNode *p=root;
            TreeNode *q=root;
            return DFS(p,q);
        }
    };
    

    另一种繁一点,先复制原树,然后翻转,再跟原树比较。

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        void copyandreverse(TreeNode *root,TreeNode *&newroot,TreeNode *p,bool flag)
        {
            if(root)
            {
                TreeNode *node=new TreeNode(root->val);
                if(newroot==NULL)
                {
                    newroot=p=node;
                }
                else
                {
                    if(flag) p->right=node;
                    else p->left=node;
                    p=node;
                }
                copyandreverse(root->left,newroot,p,true);
                copyandreverse(root->right,newroot,p,false);
            }
        }
        bool DFS(TreeNode *root,TreeNode *newTree)
        {
            if(!root&&!newTree) return true;
            else if(root&&newTree)
            {
                if(root->val!=newTree->val) return false;
                else return DFS(root->left,newTree->left)&&DFS(root->right,newTree->right);
            }
            else return false;
        }
        bool isSymmetric(TreeNode *root) {
            if(root==NULL) return true;
            TreeNode *newTree=NULL;
            TreeNode *p=newTree;
            copyandreverse(root,newTree,p,true);
            return DFS(root,newTree);
        }
    };
    
  • 相关阅读:
    三种钱是花的越多,赚的越多
    程序员除去繁华,你的匠心何在?
    科目三考试
    药房托管
    文章标题
    【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动
    Android开发时经经常使用的LogUtil
    仿支付宝/微信的password输入框效果GridPasswordView解析
    hdoj 1518 Square 【dfs】
    mysql配置文件夹错误:在安装mysql 5.6.19 时运行cmake命令是出现CMake Error: The source directory does not appear to contai
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3460489.html
Copyright © 2020-2023  润新知