• 判断二叉树是否对称


    class Solution {
    public:
        /*
        对于一颗二叉树,从根节点开始遍历
        如果左右节点有一个为NULL,那么肯定不是对称二叉树
        如果左右子节点均不为空,但不相等,那么肯定不是对称二叉树
        如果左右子节点均不为空且相等,那么:
            遍历左子树,遍历顺序为,当前节点,左子树,右子树
            遍历右子树,遍历顺序为,当前节点,右子树,左子树
        如果遍历左子树的序列和遍历右子树的序列一样,则称该二叉树为对称二叉树
    
        */
        bool isSymmetrical(TreeNode* pRoot)
        {
            if(pRoot==NULL)
                return true;
            return isMirror(pRoot->left,pRoot->right);
        }
    
        bool isMirror(TreeNode *pLeft,TreeNode *pRight){
            if(pLeft==NULL&&pRight==NULL)
                return true;
            if(pLeft==NULL||pRight==NULL)
                return false;
            if(pLeft->val!=pRight->val)
                return false;
            return isMirror(pLeft->left,pRight->right)&&isMirror(pLeft->right,pRight->left);
        }
    
    };
    class Solution {
    public:
        /*
        对于一颗二叉树,从根节点开始遍历
        如果左右节点有一个为NULL,那么肯定不是对称二叉树
        如果左右子节点均不为空,但不相等,那么肯定不是对称二叉树
        如果左右子节点均不为空且相等,那么:
            遍历左子树,遍历顺序为,当前节点,左子树,右子树
            遍历右子树,遍历顺序为,当前节点,右子树,左子树
        如果遍历左子树的序列和遍历右子树的序列一样,则称该二叉树为对称二叉树
    
        */
        bool isSymmetrical(TreeNode* pRoot)
        {
            if(pRoot==NULL)
                return true;
            return isMirror(pRoot->left,pRoot->right);
        }
    
        bool isMirror(TreeNode *pLeft,TreeNode *pRight){
            if(pLeft==NULL&&pRight==NULL)
                return true;
            if(pLeft==NULL||pRight==NULL)
                return false;
            if(pLeft->val!=pRight->val)
                return false;
            return isMirror(pLeft->left,pRight->right)&&isMirror(pLeft->right,pRight->left);
        }
    
    };
    
  • 相关阅读:
    ORA-01935: missing user or role name
    ORA-00923: FROM keyword not found where expected
    ORA-00933: SQL command not properly ended
    ORA_12514:TNS:listener does not currently know of service requested in connect descriptor
    ORA-00918: column ambiguously defined
    ORA-01789: 查询块具有不正确的结果列数
    ORA-01789: query block has incorrect number of result columns
    ORA-01747
    ORA-01843: not a valid month
    ORA-00904: "I_LEVEL": invalid identifier
  • 原文地址:https://www.cnblogs.com/bananaa/p/7541814.html
Copyright © 2020-2023  润新知