一、题目汇总表格
| ID | Title | Difficulty | Description | Tag | Important Point |
|---|---|---|---|---|---|
| 979 | Distribute Coins in Binary Tree | Medium | 给定二叉树,求二叉树结点都是1时分配移动数 | DFS/BFS | 对于每个结点有三种情况,只提供给孩子;提供给父母;父母提供给他;提供方向不同用正负区分,递归的思路 |
| 1123 | Lowest Common Ancestor of Deepest Leaves) | Medium | 给定二叉树,求二叉树最深结点的最近公共祖先 | LCA | 求最深的结点,然后递归求解,注意h=height-1的边界条件 |
| 1302 | Deepest Leaves Sum | Medium | 给定二叉树,求二叉树最深的子节点的和 | DFS/BFS | 维护当前的深度与求和 |
| 1315 | Sum of Nodes with Even-Valued Grandparent | Medium | 给定二叉树,求二叉树结点为偶数的孙子结点的和 | DFS/BFS | 维护子节点vector |
二、AC 代码:
1.leetcode 979
#include<iostream>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int DFS(TreeNode* t,int &count){
if(t==NULL) return 0;
int child_sum = DFS(t->left,count) + DFS(t->right,count);
if(t->val==child_sum+1){//够提供给自己和孩子
return 0;
}
else if(t->val>child_sum+1){//还能提供给父母
int toParent = t->val - (child_sum + 1);
count += toParent;
return -toParent;
}
else{//需要父母提供给他
int fromParent= (child_sum + 1)-t->val;
count += fromParent;
return fromParent;
}
}
class Solution {
public:
int distributeCoins(TreeNode* root) {
int count=0;
DFS(root,count);
return count;
}
};
2.leetcode 1123
#include<iostream>
#include<algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/*LCA问题:指在有根树中,找出某两个结点u和v最近的公共祖先*/
int TreeDepth(TreeNode* root){
if(root==NULL)
return 0;
return 1 + max(TreeDepth(root->right), TreeDepth(root->left));
}
TreeNode* DFS(TreeNode* t, int h,int height){
if(t==NULL)
return NULL;
if(h==height-1)
return t;
TreeNode *l = DFS(t->left, h + 1, height);
TreeNode *r = DFS(t->right, h + 1, height);
if(l!=NULL&&r!=NULL){
return t;
}
if(l==NULL)
return DFS(t->right, h + 1, height);
return DFS(t->left, h + 1, height);
}
class Solution {
public:
TreeNode* lcaDeepestLeaves(TreeNode* root) {
int TreeHeight = TreeDepth(root);
return DFS(root, 0, TreeHeight);
}
};
class Solution_2 {
public:
TreeNode* lcaDeepestLeaves(TreeNode* root) {
if(root==NULL)
return NULL;
int l = TreeDepth(root->left);
int r = TreeDepth(root->right);
if(l==r)
return root;
else if (l>r)
return lcaDeepestLeaves(root->right);
else
return lcaDeepestLeaves(root->left);
}
};
3.leetcode 1302
#include<iostream>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void DFS(TreeNode* t, int n,int& d_sum,int& height){
if(t==NULL) return;
if(n==height)
d_sum += t->val;
else if(n>height){
d_sum = t->val;
height = n;
}
DFS(t->left, n + 1, d_sum, height);
DFS(t->right, n + 1, d_sum, height);
}
class Solution {
public:
int deepestLeavesSum(TreeNode* root) {
int d_sum = 0;
int height = 1;
DFS(root, 1, d_sum, height);
return d_sum;
}
};
4.leetcode 1315
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void DFS(TreeNode* t,vector<TreeNode*>& e_child){
if(t==NULL) return;
if(t->val%2==0){
if(t->left!=NULL)
e_child.push_back(t->left);
if(t->right!=NULL)
e_child.push_back(t->right);
}
DFS(t->left, e_child);
DFS(t->right,e_child);
}
class Solution {
public:
int sumEvenGrandparent(TreeNode* root) {
vector<TreeNode*> e_child;
int e_sum=0;
DFS(root, e_child);
for (int i = 0; i < e_child.size();i++){
if(e_child[i]->left!=NULL)
e_sum += e_child[i]->left->val;
if(e_child[i]->right!=NULL)
e_sum += e_child[i]->right->val;
}
return e_sum;
}
};
四、总结:
DFS和BFS的题目还是比较常考的,但是可以按照模板来做,整体来说难度不大。
本文正在参与「掘金 2021 春招闯关活动」, 点击查看 活动详情