100. Same Tree
判定为same tree,由结构+val决定。
使用DFS,先到达最下方的叶节点,判断是否都为nullptr,然后再结合节点的val值。
对于树,由于会涉及比对val,所以先把节点为空的情况判断完全。
此题中,1、两点均为空节点。即相同且已经走到了叶节点,返回true(之前都没return false说明之前符合,到这一步也就符合了)
2、两节点有一个为空节点。返回false,因为两者结构不同。
3、两节点不为空。接下来,在两节点都不为空的情况下,才可以判断val。
若val不同,也返回false。若相同,则继续向下。
若走到这一步,说明是两个节点都有值且之前都是符合题意的,接下来分别对两个节点的左节点、两个节点的右节点进行同样的判定。直到走到叶节点(true)或者不符合某个条件
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) // 两个节点均为空,即顺利走到了叶节点
return true;;
if(!p || !q) // 有一个为空,即结构不同
return false;
if ((p->val != q->val) // val不同,即值不同
return false;
bool left = isSameTree(p->left, q->left);
bool right = isSameTree(p->right, q->right);
return left && right;
}
};
104. Maximum Depth of Binary Tree
如果一个节点的【子节点】为nullptr,则认为该节点深度为1。每个节点的深度等于左右子树的深度的最大值+1。
先走到叶节点,到达叶节点的子节点时,其为空,则返回0,之后层层取左右最深度再+1
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root) return 0; // 当前节点为空,则深度为0
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return(max(leftDepth,rightDepth)+1);
}
};
110. Balanced Binary Tree
判断平衡二叉树即判断其左右子树是否为平衡二叉树。
首先若根节点为空,则为平衡二叉树。
对左右子树,判断两树的深度是否相差1以上,是的话即返回false。若能走到根节点则返回true。
分别对左右子树判断。
class Solution {
public:
int maxDepth(TreeNode *root)
{
if(!root)
return 0;
return max(maxDepth(root->left), maxDepth(root->right))+1;
}
bool isBalanced(TreeNode *root) {
if(!root)
return true;
if(abs(maxDepth(root->left)-maxDepth(root->right)) > 1)
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
};
111. Minimum Depth of Binary Tree
min相对max的问题,如果直接不加判断地使用min(左,右)会导致如果一边子树为空,会把这边的长度算进来,事实并未到达叶节点。
要对这种情况加以区分:如果没有left,长度即等于right的min+1。
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == nullptr)
return 0;
if(root->left == nullptr)
return 1 + minDepth(root->right);
if(root->right == nullptr)
return 1 + minDepth(root->left);
return 1+min(minDepth(root->left),minDepth(root->right));
}
};
112. Path Sum
求是否存在路径的val和等于给定的sum。
判断true:当某个节点的左右子节点均为空且其值等于剩下的sum数
判断false:节点为nullptr
任有一条路径为true,结果即为true
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root==nullptr)
return false;
if(root->left == nullptr && root->right == nullptr && root->val == sum)
return true;
bool left = hasPathSum(root->left, sum-root->val);
bool right = hasPathSum(root->right, sum-root->val);
return left || right;
}
};
101. Symmetric Tree
判断镜像树,即判断根节点的左右子树是否镜像。
false: 1,左右单独有一个为空(结构不同);2, 左右对称处val不同
true:左右均为空
class Solution {
public:
bool subIsSymmetric(TreeNode* p, TreeNode* q) {
if(!q && !p)
return true;
if(!q || !p)
return false;
if(p->val != q->val)
return false;
return subIsSymmetric(p->left, q->right) && subIsSymmetric(p->right , q->left);
}
bool isSymmetric(TreeNode* root) {
if(!root)
return true;
return subIsSymmetric(root->left, root->right);
}
};
257. Binary Tree Paths
得到树的所有路径的值,以a->b->c的字符串形式。
终止条件:叶节点。
一定注意,终止为为叶节点和终止为nullptr不同。某些节点只有一个子节点,如果仅判断下一个是否为nullptr则会误判。
每个节点都写入值,直到到达叶节点,存入作为一个path。
在往左右延伸递归的时候,先判断是否有这一边的子节点。只往还有子节点的路走,直到没有子节点(叶)。
class Solution {
public:
void getPath(vector<string>& res, TreeNode* root, string s) {
if(!root->left && !root->right) {
res.push_back(s);
return;
}
if(root->left){
getPath(res, root->left, s + "->" + to_string(root->left->val));
}
if(root->right){
getPath(res, root->right, s + "->" + to_string(root->right->val));
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root)
return res;
getPath(res, root, to_string(root->val);
return res;
}
};
690. Employee Importance
695. Max Area of Island
逐个元素扫描,当==1时,则开始计算该区域面积。
计算面积:(i, j)点所在的面积等于其上下左右点的面积相加再加一(本身占面积为1),而每个其他点的面积同样,递归求解。每个算过的点置零,节约操作。 当点走到边界或者其值不为1时,则返回0。
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int maxAreaSize = 0;
int currAreaSize = 0;
for(int i=0; i<grid.size(); i++)
{
for(int j=0; j<grid[0].size(); j++)
{
if(grid[i][j] == 1)
{
currAreaSize = areaOfIsland(grid, i, j);
maxAreaSize = max(maxAreaSize, currAreaSize);
}
}
}
return maxAreaSize;
}
int areaOfIsland(vector<vector<int>>& grid, int i, int j)
{
if(i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size() && grid[i][j] == 1)
{
grid[i][j] = 0;
return 1+areaOfIsland(grid, i-1, j)+areaOfIsland(grid, i+1, j)+areaOfIsland(grid, i, j-1)+areaOfIsland(grid, i, j+1);
}
return 0;
}
};
733. Flood Fill
选择一个起点,将所有和这个点连接的且值相同的点的值换为新值。可得到要递归操作的点的判定条件:
1、在区域内( i 与 j 不小于0,不大于size();
2、其值等于欲操作的值(image[i][j]== preColor)
符合该条件的点,将值替换,再对四面的点递归操作。
class Solution {
public:
void fill(vector<vector<int>>& image, int i, int j, int newColor, int preColor) {
if(i>=0 && i<image.size() && j>=0 && j<image[0].size() && image[i][j] == preColor) {
image[i][j] = newColor;
fill(image, i+1, j, newColor, preColor);
fill(image, i-1, j, newColor, preColor);
fill(image, i, j+1, newColor, preColor);
fill(image, i, j-1, newColor, preColor);
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(image[sr][sc] == newColor)
return image;
int preColor = image[sr][sc];
fill(image, sr, sc, newColor, preColor);
return image;
}
};