九. 树
题目分类 题目编号
树与递归 100、222、101、226、437、563、617、508、572、543、654、687、87
树的层次遍历 102、429、690、559、662、671、513、515、637、103、107、257、623、653、104、111、112、113、129、404、199、655、116、117
树的前序遍历 144、589
树的前序序列化 606、331、652、297、449
树的后序遍历 145、590
树的中序遍历与二叉搜索树 94、700、530、538、230、98、173、669、450、110、95、108、109
重构二叉树 105、106
二叉树的展开 114
最近公共祖先 235、236
Morris中序遍历 501、99
四叉树 558、427
222. 完全二叉树的节点个数
101. 对称二叉树
226. 翻转二叉树
first:
注意
invertTree(root->left);
后,最终返回nullptr,但依然会顺序执行下一行
sec:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
swap(root);
return root;
}
void swap(TreeNode* root){
if(root==nullptr) return;
TreeNode* t=root->left;
root->left=root->right;
root->right=t;
swap(root->left);
swap(root->right);
}
};
分成两部分
437. 路径总和 III
class Solution {
public://先定义一个函数,获得某个节点为根节点的时候的路径总数,然后主函数遍历所有节点。
int rootSum(TreeNode* root, long targetSum) {
if(!root) return 0;
int ret=0;
if(root->val==targetSum){
ret++;
}
ret += rootSum(root->left, targetSum - root->val);
ret += rootSum(root->right, targetSum - root->val);
return ret;
}
int pathSum(TreeNode* root, int targetSum) {
if(!root) return 0;
int a= rootSum(root,targetSum);
a=a+ pathSum(root->left,targetSum);
a=a+ pathSum(root->right,targetSum);
return a;
}
};
102. 二叉树的层序遍历
我的方法:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector <vector <int>> V;
if (!root) {
return V;
}
vector<int>v;
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int a = q.size();
for (int i = 0; i<a; ++i) {
auto node = q.front(); q.pop();
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
v.push_back(node->val);
}
V.push_back(v);
v.clear();
}
return V;
}
};
class Solution {
public:
vector<vector<int>> re;
vector<vector<int>> levelOrder(TreeNode* root) {
dfs(root,0);
for(int i=re.size()-1;i>=0;i--)//去除掉为空的部分。
{
if(re[i].size()==0)
re.pop_back();
}
return re;
}
void dfs(TreeNode* root,int index)
{
if(root==nullptr)
return;
vector<int> ve;
re.push_back(ve);//如果下一层为空,此处会多加一个空vector,需要去除。
dfs(root->left,index+1);
re[index].push_back(root->val);//向层数是index的结果集中插入
dfs(root->right,index+1);
}
};
上图为DFS 深度优先遍历
429. N 叉树的层序遍历
注意用for(a:b)遍历
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>>V;
if(root==nullptr) return V;
vector<int>v;
queue<Node*>q;
q.push(root);
while(!q.empty()){
int size=q.size();
for(int i=0;i<size;i++){
auto node=q.front();
q.pop();
v.push_back(node->val);
for(auto b:node->children){
q.push(b);
}
}
V.push_back(v);
v.clear();
}
return V;
}
};
559. N 叉树的最大深度
DFS:
class Solution {
public:
int maxDepth(Node* root) {
if (root == nullptr) {
return 0;
}
int maxChildDepth = 0;
for (auto child : root->children) {
maxChildDepth = max(maxChildDepth, maxDepth(child));
}
return maxChildDepth + 1;
}
};
662. 二叉树最大宽度
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
unsigned long long cnt = 1;
vector<pair<TreeNode *, unsigned long long>> arr;
arr.emplace_back(root, 1ULL);
while (!arr.empty()) {
vector<pair<TreeNode *, unsigned long long>> tmp;
for (auto [node, index] : arr) {
if (node->left) {
tmp.emplace_back(node->left, index * 2);
}
if (node->right) {
tmp.emplace_back(node->right, index * 2 + 1);
}
}
cnt = max(cnt, arr.back().second - arr.front().second + 1);
arr = move(tmp);
}
return cnt;
}
};
1.emplace_back():只调用构造函数;
push_back():调用构造函数和拷贝构造函数;
2.用vector装对值,index记录编号,move函数拷贝
3.为什么不用map,因为这题用不上用键去访问对应的值。