zoukankan      html  css  js  c++  java
  • 【easy】101. Symmetric Tree

    判断一棵二叉树是否对称

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    //前序遍历==对称前序遍历
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            return isSymmetric(root,root);
        }
        
        bool isSymmetric(TreeNode* pRoot1,TreeNode* pRoot2){
            if (pRoot1 == NULL && pRoot2 == NULL)
                return true;
            if (pRoot1 == NULL || pRoot2 == NULL)
                return false;
            if (pRoot1->val != pRoot2->val)
                return false;
            return isSymmetric(pRoot1->left, pRoot2->right) && isSymmetric(pRoot2->left, pRoot1->right);
        }
    };
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    //前序遍历==对称前序遍历
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            return isSymmetric(root,root);
        }
        
        bool isSymmetric(TreeNode* pRoot1,TreeNode* pRoot2){
            if (pRoot1 == NULL && pRoot2 == NULL)
                return true;
            if (pRoot1 == NULL || pRoot2 == NULL)
                return false;
            if (pRoot1->val != pRoot2->val)
                return false;
            return isSymmetric(pRoot1->left, pRoot2->right) && isSymmetric(pRoot2->left, pRoot1->right);
        }
    };
  • 相关阅读:
    MiniOS系统
    《硅谷传奇》
    《构建之法》1—3章
    学术诚信与职业道德
    Sprint2
    Scrum 项目 7.0 Sprint回顾
    Scrum 项目 6.0 sprint演示
    Scrum 项目 5.0
    Scrum 项目4.0
    操作系统 实验三 进程调度模拟程序
  • 原文地址:https://www.cnblogs.com/sherry-yang/p/8445671.html
Copyright © 2011-2022 走看看