1. 矩阵中的路径
题目:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/
思路:同回溯法(一)单词搜索
class Solution {
private:
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int m,n;
public:
bool exist(vector<vector<char>>& board, string word) {
m=board.size();
n=board[0].size();
vector<vector<bool>> visited(m,vector<bool>(n,false));
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(dfs(board, word, i, j, 0, visited)){
return true;
}
}
}
return false;
}
bool dfs(vector<vector<char>>& board, string& word, int i, int j, int u, vector<vector<bool>>& visited){
//if(u==word.size()) return true; 思考:为啥这里不可以?
if(u==word.size()-1) return board[i][j]==word[u];
if(board[i][j]==word[u]){
visited[i][j]=true;
for(int k=0; k<4; k++){
int new_i=i+dx[k];
int new_j=j+dy[k];
if(inArea(new_i,new_j) && !visited[new_i][new_j]){
if(dfs(board, word, new_i, new_j, u+1, visited))
return true;
}
}
visited[i][j]=false;
}
return false;
}
bool inArea(int x, int y){
return x>=0 && x<m && y>=0 && y<n;
}
};
思考处失败案例:
[["a"]]
"a"
2. 二叉树中和为某一值的路径
题目:https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
思路:
先序遍历: 按照 “根、左、右” 的顺序,遍历树的所有节点。
路径记录: 在先序遍历中,记录从根节点到当前节点的路径。当路径为 ① 根节点到叶节点形成的路径 且 ② 各节点值的和等于目标值 sum 时,将此路径加入结果列表。
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> path;
dfs(root,sum,path,res);
return res;
}
void dfs(TreeNode* root, int sum, vector<int>& path, vector<vector<int>>& res){
if(root==nullptr) return;
path.push_back(root->val);
sum -= root->val;
if(root->left==nullptr && root->right==nullptr && sum==0){
res.push_back(path);
}
dfs(root->left, sum, path, res);
dfs(root->right, sum, path, res);
path.pop_back();
}
};
3. 二叉树的深度
题目:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
思路:
方法1:递归,二叉树,一般先想想递归方法
方法2:迭代,用层序遍历的方法,思路是,遍历1
层,深度+1
//迭代
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> q;
if(root) q.push(root);
int depth = 0;
while (q.size()) {
int n = q.size();
for(int i = 0; i < n; i ++) {
TreeNode* tr = q.front();
q.pop();
if(tr->left) q.push(tr->left);
if(tr->right) q.push(tr->right);
}
depth ++;
}
return depth;
}
};
//递归
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr) return 0;
int leftDepth=maxDepth(root->left);
int rightDepth=maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth+1 : rightDepth+1;
}
};
4. 平衡二叉树
题目:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/
思路:
(1)可参考上题
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
return abs(treeDepth(root->left)-treeDepth(root->right))<=1
&&isBalanced(root->left) && isBalanced(root->right);
}
int treeDepth(TreeNode* root){
if(root==nullptr) return 0;
int left=treeDepth(root->left);
int right=treeDepth(root->right);
return left<right?right+1:left+1;
}
};
(2) 后序遍历+剪枝
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
return Depth_diff(root) != -1;
}
int Depth_diff(TreeNode* root){
if(root==nullptr) return 0;
int left=Depth_diff(root->left);
if(left==-1) return -1; //左子树深度为-1,代表左子树不是平衡树,直接剪枝
int right=Depth_diff(root->right);
if(right==-1) return -1; //判断右子树……
return abs(left-right)<2 ? max(left,right)+1 : -1;
}
};