代码随想录刷题第14天

代码随想录刷题第14天

二叉树的层序遍历

/*
 * @lc app=leetcode.cn id=102 lang=cpp
 *
 * [102] 二叉树的层序遍历
 */

// @lc code=start
/**
 * 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) {}
 * };
 */
#include <vector>
#include <iostream>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };
class Solution {
public:
    void transation(TreeNode* root,int depth,vector<vector<int>> &result){
        if (root == nullptr)
        {
            return ;
        }
        if (result.size() == depth)
        {
            result.push_back(vector<int>());
        }
        result[depth].push_back(root->val);
        transation(root->left,depth+1,result);
        transation(root->right,depth+1,result);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> result;
        //int depth = 0;
        transation(root,0,result);
        return result;
    }
};
// @lc code=end


递归法,寻找递归条件,然后每一层插入一个数组,相当于深度优先遍历然后插入,从而变成纵向遍历~

翻转二叉树

/*
 * @lc app=leetcode.cn id=226 lang=cpp
 *
 * [226] 翻转二叉树
 */

// @lc code=start
/**
 * 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) {}
 * };
 */

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };

class Solution {
public:
    void transation(TreeNode* root){
        if (root == nullptr)
        {
            return ;
        }
        swap(root->left,root->right);
        transation(root->left);
        transation(root->right);
        
    }
    TreeNode* invertTree(TreeNode* root) {
        transation(root);
        return root;
    }
};
// @lc code=end


简单的翻转左右孩子!

对称二叉树

/*
 * @lc app=leetcode.cn id=101 lang=cpp
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * 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) {}
 * };
 */

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };


class Solution {
public:
    bool transation(TreeNode* left,TreeNode* right){
        if (left == NULL&& right == NULL)
        {
            return true;
        }else if (left != NULL&& right == NULL)
        {
            return false;
        }else if (left == NULL&& right!= NULL)
        {
            return false;
        }else if (left->val  != right->val)
        {
            return false;
        }
        bool out =  transation(left->left,right->right);
        bool in = transation(left->right,right->left);
        bool final = out && in;
        return final;
    }


    bool isSymmetric(TreeNode* root) {
        if (root == nullptr)
        {
            return true;
        }
        
        bool result = transation(root->left,root->right);
        return result;
    }
};
// @lc code=end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值