• [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);
        }
    };
    
  • 相关阅读:
    ltp-ddt makefile的思考
    Linux configure,make,make install
    linux下can调试工具canutils安装过程记录
    windows下运行jar
    悲观锁
    mysql事务锁表
    静态内部类
    局部类
    匿名内部类(new类时覆盖类中方法)
    回文字
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3460489.html
Copyright © 2020-2023  润新知