102. 二叉树的层序遍历+226. 翻转二叉树+101. 对称二叉树

102. 二叉树的层序遍历

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        vector<int> hang;
        queue<TreeNode*> temp;
        temp.push(root);
        if(root == NULL)
        {
            return res;
        }
        while(!temp.empty())
        {
            hang.resize(0);
            int size = temp.size();
            for(int i = 0 ; i < size ; i++)
            {
                root = temp.front();
                hang.push_back(root -> val);
                temp.pop();
                if(root-> left != NULL) temp.push(root -> left);
                if(root-> right != NULL) temp.push(root -> right);
            }
            res.push_back(hang);
        }
        return res;
    }
};

226. 翻转二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == NULL) return root;
        swap(root->left , root -> right );
        invertTree(root -> right);
        invertTree(root -> left);
        return root;
    }
};

其实这道题虽然用的是递归,但是只有第一次迭代的返回值有效。

101. 对称二叉树

递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool myfun(TreeNode* le, TreeNode* ri)
    {
        if(le == NULL && ri!= NULL) return 0;
        else if(le != NULL && ri == NULL) return 0;
        else if(le != NULL && ri != NULL)
        {
            if(le -> val != ri -> val) return 0;
            return myfun(le->left, ri -> right)&&myfun(le->right , ri->left);
        }
        else return 1;
    }
    bool isSymmetric(TreeNode* root) {
        int res = 1;
        if(root == NULL) return res; 
        res = myfun(root -> left , root -> right );
        return res;
    }
};

迭代

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue <TreeNode*> temp;
        TreeNode* leftcode;
        TreeNode* rightcode;
        if(root == NULL) return 1;
        temp.push(root -> left);
        temp.push(root -> right);
        while(!temp.empty())
        {
            leftcode = temp.front() ; 
            temp.pop();
            rightcode = temp.front();
            temp.pop();
            if(leftcode == NULL && rightcode != NULL) return 0;
            else if(leftcode != NULL && rightcode == NULL) return 0;
            else if(leftcode != NULL && rightcode != NULL) 
            {
                if(leftcode -> val != rightcode -> val) return 0;
                temp.push(leftcode -> left);
                temp.push(rightcode -> right);
                temp.push(leftcode -> right);
                temp.push(rightcode -> left);
            }

        }
        return 1;
    }
};

100. 相同的树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p == NULL && q!= NULL ) return 0;
        else if(p != NULL && q == NULL )return 0;
        else if(p != NULL && q != NULL)
        {
            if(p -> val != q -> val )return 0;
            return isSameTree(p->left , q-> left)&&isSameTree(p-> right , q->right);
        }
        else return 1;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值