11.acwing38. 二叉树的镜像
class Solution {
public:
void mirror(TreeNode* root) {
if(!root) return;
swap(root->left, root->right);
mirror(root->left);
mirror(root->right);
return ;
}
};
12.acwing46. 二叉搜索树的后序遍历序列
class Solution {
public:
vector<int> post;
bool verifySequenceOfBST(vector<int> sequence) {
post = sequence;
return dfs(0, post.size()-1);
}
bool dfs(int pl, int pr){
if(pl >= pr) return true;
int r = post[pr], k = pl;
while(k < pr && post[k] < r) k ++;
while(k < pr) if(post[k++] < r) return false;
return dfs(pl, k-1) && dfs(k+1, pr-1);
}
};
13.AcWing 47. 二叉树中和为某一值的路径
class Solution {
public:
int sum;
vector<int> path;
vector<vector<int>> ans;
void dfs(TreeNode* root, int num){
if(!root) return ;
int t = num + root->val;
path.push_back(root->val);
if(t > sum){
path.pop_back();
return ;
}
else if(t == sum && !root->left && !root->right){
ans.push_back(path);
}
dfs(root->left, num + root->val);
dfs(root->right, num + root->val);
path.pop_back();
}
vector<vector<int>> findPath(TreeNode* root, int _sum) {
sum = _sum;
dfs(root, 0);
return ans;
}
};
14.LeetCode297. 二叉树的序列化与反序列化
这里选用的是先序遍历序列化
class Codec {
public:
void dfs1(TreeNode* root, string& s){
if(!root){
s += "$,";
return ;
}
s += to_string(root->val) + ',';
dfs1(root->left, s);
dfs1(root->right, s);
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs1(root, res);
return res;
}
TreeNode* dfs2(string& s, int& u){
if(s[u] == '$'){
u += 2;
return NULL;
}
bool is_minus = s[u] == '-';
if(is_minus) u ++;
int num, st = u;
while(s[u] != ',') u ++;
num = stoi(s.substr(st, u-st));
if(is_minus) num = -num;
u++;
auto root = new TreeNode(num);
root->left = dfs2(s, u);
root->right = dfs2(s, u);
return root;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int u = 0;
return dfs2(data, u);
}
};
15.AcWing49. 二叉搜索树与双向链表
利用中序遍历将二叉搜索树转化为双向链表
class Solution {
public:
TreeNode* pre;
TreeNode* convert(TreeNode* root) {
dfs(root);
while(root && root->left) root = root->left;
return root;
}
void dfs(TreeNode* u){
if(!u) return ;
dfs(u->left);
u->left = pre;
if(pre) pre->right = u;
pre = u;
dfs(u->right);
}
};
16.AcWing70. 二叉搜索树的第k个结点
中序遍历中加个计数器就行,找到第k个就停。
class Solution {
public:
TreeNode* ans;
void inorder(TreeNode* u, int& k){
if(!u) return ;
inorder(u->left, k);
k--;
if(!k){
ans = u;
return ;
}
inorder(u->right, k);
}
TreeNode* kthNode(TreeNode* root, int k) {
inorder(root, k);
return ans;
}
};
17.AcWing 37. 树的子结构
枚举树一的每一个子节点,与树二进行比较。若根节点相同,则按同样的顺序遍历两棵树。
class Solution {
public:
bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if(!pRoot1 || !pRoot2) return false;
if(is_same(pRoot1, pRoot2)) return true;
return hasSubtree(pRoot1->left, pRoot2) || hasSubtree(pRoot1->right, pRoot2);
}
bool is_same(TreeNode* a, TreeNode* b){
if(!b) return true;
if(!a || a->val != b->val) return false;
return is_same(a->left, b->left) && is_same(a->right, b->right);
}
};
18.LeetCode572. 另一个树的子树
枚举树一的每一个子节点,与树二进行比较。若根节点相同,则按同样的顺序遍历两棵树。不同于AcWing 37. 树的子结构 的是这题是看 t
树是否是 s 树的子树而不是一部分。
class Solution {
public:
bool isSame(TreeNode* s, TreeNode* t){
if(!s || !t) return !s && !t;
if(s->val != t->val) return false;
return isSame(s->left, t->left) && isSame(s->right, t->right);
}
bool isSubtree(TreeNode* s, TreeNode* t) {
if(!s || !t) return false;
if(isSame(s, t)) return true;
return isSubtree(s->left, t) || isSubtree(s->right, t);
}
};