代码随想录刷题第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