zoukankan      html  css  js  c++  java
  • [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);
        }
    };
    
  • 相关阅读:
    WinowsXP 任务栏无法显示当前运行程序图标
    日志记录组件[Log4net]详细介绍(转)
    桌面上的IE图标变成了快捷方式那种图标 怎么还原回来
    面试必须要知道的SQL语法,语句(转载)
    兼容 火狐 IE 的JS时间控件 任意格式 年月日时分秒
    Nagios远程监控软件的安装与配置详解(1)
    linux集群 负载均衡实验笔记
    PHPB2B 模板 标签
    PHP 去除 HTML 回车 换行 空格
    OpenX参考网址
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3460489.html
Copyright © 2011-2022 走看看