513.找树左下角的值
思路与重点
- 我的解法:直接层序遍历,最后一层的第一个节点值即为所求。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int ans = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i = 0; i < size; i++){
TreeNode* node = q.front();
q.pop();
if(i == 0) ans = node->val;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return ans;
}
};
- 也可以使用回溯法解决:当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth) {
if (root->left == NULL && root->right == NULL) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
return;
}
if (root->left) {
traversal(root->left, depth + 1);
}
if (root->right) {
traversal(root->right, depth + 1);
}
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return result;
}
};
112. 路径总和
思路与重点
- 我的思路:递归遍历遇到叶子节点时进行比较,存在的问题是没有及时退出函数。
class Solution {
public:
void traversal(TreeNode* cur, int pathSum, int targetSum, bool& flag){
if(!cur->left && !cur->right && pathSum + cur->val == targetSum){
flag = true;
return;
}
if(cur->left) traversal(cur->left, pathSum + cur->val, targetSum, flag);
if(cur->right) traversal(cur->right, pathSum + cur->val, targetSum, flag);
}
bool hasPathSum(TreeNode* root, int targetSum) {
bool ans = false;
int pathSum = 0;
if(!root) return ans;
traversal(root, pathSum, targetSum, ans);
return ans;
}
};
- **递归函数什么时候需要返回值?什么时候不需要返回值?**这里总结如下三点:
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
- 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
- 代码随想录中的写法:
class Solution {
private:
bool traversal(TreeNode* cur, int count) {
if (!cur->left && !cur->right && count == 0) return true;
if (!cur->left && !cur->right) return false;
if (cur->left) {
count -= cur->left->val;
if (traversal(cur->left, count)) return true;
count += cur->left->val;
}
if (cur->right) {
count -= cur->right->val;
if (traversal(cur->right, count)) return true;
count += cur->right->val;
}
return false;
}
public:
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
return traversal(root, sum - root->val);
}
};
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (!root) return false;
if (!root->left && !root->right && sum == root->val) {
return true;
}
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};
class solution {
public:
bool haspathsum(TreeNode* root, int sum) {
if (root == null) return false;
stack<pair<TreeNode*, int>> st;
st.push(pair<TreeNode*, int>(root, root->val));
while (!st.empty()) {
pair<TreeNode*, int> node = st.top();
st.pop();
if (!node.first->left && !node.first->right && sum == node.second) return true;
if (node.first->right) {
st.push(pair<TreeNode*, int>(node.first->right, node.second + node.first->right->val));
}
if (node.first->left) {
st.push(pair<TreeNode*, int>(node.first->left, node.second + node.first->left->val));
}
}
return false;
}
};
class Solution {
public:
void traversal(TreeNode* root, int targetSum, vector<vector<int>>& ans, vector<int> path){
path.push_back(root->val);
if(!root->left && !root->right && targetSum == root->val){
ans.push_back(path);
}
if(root->left){
traversal(root->left, targetSum - root->val, ans, path);
}
if(root->right){
traversal(root->right, targetSum - root->val, ans, path);
}
path.pop_back();
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> ans;
vector<int> path;
if(root) traversal(root, targetSum, ans, path);
return ans;
}
};
106.从中序与后序遍历序列构造二叉树
思路与重点
- 中序序列可以与先序序列、后序序列、层序序列中的任意一个来构建唯一的二叉树,而后三者两两搭配或是三个一起上都无法构建唯一的二叉树。原因是先序、后序、层序的作用均是提供根节点,都必须由中序序列来区分出左右子树。
- 全部使用闭区间的写法,参见《算法笔记》:
class Solution {
public:
TreeNode* traversal(vector<int>& inorder, vector<int>& postorder, int inL, int inR, int postL, int postR){
if(postL > postR) return NULL;
TreeNode* root = new TreeNode(postorder[postR]);
int delimiterIndex;
for(delimiterIndex = inL; delimiterIndex <= inR; delimiterIndex++){
if(inorder[delimiterIndex] == postorder[postR]) break;
}
int numLeft = delimiterIndex - inL;
root->left = traversal(inorder, postorder, inL, delimiterIndex - 1, postL, postL + numLeft - 1);
root->right = traversal(inorder, postorder, delimiterIndex + 1, inR, postL + numLeft, postR - 1);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return traversal(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
}
};
class Solution {
private:
TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& postorder, int postorderBegin, int postorderEnd) {
if (postorderBegin == postorderEnd) return NULL;
int rootValue = postorder[postorderEnd - 1];
TreeNode* root = new TreeNode(rootValue);
if (postorderEnd - postorderBegin == 1) return root;
int delimiterIndex;
for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;
int rightInorderBegin = delimiterIndex + 1;
int rightInorderEnd = inorderEnd;
int leftPostorderBegin = postorderBegin;
int leftPostorderEnd = postorderBegin + delimiterIndex - inorderBegin;
int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);
int rightPostorderEnd = postorderEnd - 1;
root->left = traversal(inorder, leftInorderBegin, leftInorderEnd, postorder, leftPostorderBegin, leftPostorderEnd);
root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};