1.dfs
/**
* 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:
long pre=LONG_MIN;
bool isValidBST(TreeNode* root) {
if(!root) return true;
bool l=isValidBST(root->left);
if(root->val<=pre){//中序遍历
return false;
}
pre=root->val;
bool r=isValidBST(root->right);
return l&&r;
}
};
2.dfs(Morris)
/**
* 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:
void recoverTree(TreeNode* root) {
TreeNode* first=NULL,* second,* last=NULL;
//中序遍历序列里会有俩个逆序对,
//只需交换第一个逆序对的左边和第二个逆序对的右边。
//如果是相邻交换导致只有一个逆序对,那直接交换
while(root){
if(!root->left){//没有左子树
if(last&&last->val>root->val){
//看前继节点和当前比是否逆序
if(!first) first=last,second=root;
else second=root;
}
last=root;//往后走
root=root->right;
}else{
auto p=root->left;//p进左子树探路
while(p->right&&p->right!=root) p=p->right;
//p找左子树的最右边节点
if(!p->right) p->right=root,root=root->left;
//第一次探路找到之后连一条边,root开始遍历左子树
else{
p->right=NULL;//第二次p探路遍历到,拆路
if(last&&last->val>root->val){
if(!first) first=last,second=root;
else second=root;
}
last=root;
root=root->right;
}
}
}
swap(first->val,second->val);
}
};
3.bfs
/**
* 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;
queue<TreeNode*> q;
if(root) q.push(root);
while(q.size()){
vector<int> level;
int len=q.size();
while(len--){
auto t=q.front();
q.pop();
level.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(level);
}
return res;
}
};
4.bfs
/**
* 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>> res;
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;//层次遍历
queue<TreeNode*> q;
if(root) q.push(root);
int cnt=0;//记录层数
while(q.size()){
vector<int> level;
int len=q.size();
while(len--){
auto t=q.front();
q.pop();
level.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
if( ++ cnt % 2==0) reverse(level.begin(),level.end());//看第几层
res.push_back(level);
}
return res;
}
};
5.dfs
/**
* 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:
map<int,int> hash;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
for(int i=0;i<inorder.size();i++) hash[inorder[i]]=i;
//记录中序遍历中每个数的位置
return build(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}
TreeNode* build(vector<int>& preorder, vector<int>& inorder
,int pl, int pr, int il, int ir) {
if (pl > pr) return nullptr;
int k=hash[preorder[pl]];//中序的位置拿出来
TreeNode* root=new TreeNode(preorder[pl]);
//画图看看吧
root->left=build(preorder,inorder,pl+1,pl+k-il,il,k-1);
root->right=build(preorder,inorder,pl+k-il+1,pr,k+1,ir);
return root;
}
};
6.dfs
/**
* 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:
map<int,int> hash;
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
for(int i=0;i<inorder.size();i++) hash[inorder[i]]=i;
//记录中序遍历中每个数的位置
return build(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
}
TreeNode* build(vector<int>& inorder, vector<int>& postorder
,int il, int ir, int pl, int pr) {
if (pl > pr) return nullptr;
int k=hash[postorder[pr]];//中序的位置拿出来
TreeNode* root=new TreeNode(postorder[pr]);
//画图看看吧
root->left=build(inorder,postorder,il,k-1,pl,pl+k-il-1);
root->right=build(inorder,postorder,k+1,ir,pl+k-il,pr-1);
return root;
}
};
7.bfs
/**
* 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>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
if(root) q.push(root);
int cnt=0;
while(q.size()){
vector<int> level;
int len=q.size();
while(len--){
auto t=q.front();
q.pop();
level.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(level);
}
reverse(res.begin(),res.end());
return res;
}
};
8.dfs
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* 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* sortedListToBST(ListNode* head) {
if(!head) return NULL;
int n=0;
for(auto p=head;p;p=p->next) n++;
if(n==1) return new TreeNode(head->val);
auto cur=head;
for(int i=0;i<n/2-1;i++){//找到根节点前面一个点
cur=cur->next;
}
auto root=new TreeNode(cur->next->val);
root->right=sortedListToBST(cur->next->next);//右边
cur->next=NULL;//左子树最右点置空
root->left=sortedListToBST(head);//左边
return root;
}
};
9.dfs
/**
* 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>> res;
vector<int> path;
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(root) dfs(root,targetSum);
return res;
}
void dfs(TreeNode* root, int sum){
if(!root) return;
path.push_back(root->val);
sum-=root->val;
if(!root->left&&!root->right){//叶子
if(!sum){//0
res.push_back(path);
}
}else{
dfs(root->left,sum);
dfs(root->right,sum);
}
path.pop_back();
}
};
10.找规律
/**
* 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:
void flatten(TreeNode* root) {
while(root){
auto p=root->left;
if(p){
while(p->right) p=p->right;
//左子树的右链的最右边
p->right=root->right;//接入右子树
root->right=root->left;//左子树的右链的最右边
root->left=NULL;
}
root=root->right;
}
}
};