1.找树左下角的值
思路:
1.层序(迭代法)会更方便。
2.递归法,本题并不涉及“中”的处理逻辑,所以,前中后序都可以。只要保证“左”的处理优先于“右”的处理即可,保证优先左边搜索。
3.我们来分析一下题目:在树的最后一行找到最左边的值。
如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。所以要找深度最大的叶子节点。
4.注意回溯过程!
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* cur ,int depth){
if(cur -> left == NULL && cur -> right == NULL){
if(depth > maxDepth){
maxDepth = depth;
result = cur -> val;
}
return ;
}
if(cur -> left){
depth++;
traversal(cur -> left,depth);
depth--;
}
if(cur -> right){
depth++;
traversal(cur -> right,depth);
depth--;
}
return ;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root,0);
return result;
}
};
2.路径总和
思路:
1.可以使用深度优先遍历的方式(本题前中后序都可以,无所谓,因为中节点也没有处理逻辑)来遍历二叉树。
2.看返回值,递归函数什么时候需要返回值?什么时候不需要返回值?这里总结如下三点:
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
- 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
3.注意回溯过程!
class Solution {
public:
bool traversal(TreeNode* cur,int count){
if(cur -> left == NULL && cur -> right == NULL && count == 0) return true;
if(cur -> left == NULL && cur -> right == NULL && count != 0) return false;
// if(cur){
// count = count - cur -> val;
// }
//如果这样的话,每次递归的时候都会减去cur的值,会导致递归过程中count的值不正确
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;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == NULL) return false;
return traversal(root,targetSum - root -> val);
}
};
1. 路径总和(2)
3. 从中序与后序遍历序列构造二叉树
思路:
1.构造步骤:
(1)后序数组为0,空结点;
(2)后序数组最后一个元素为结点元素;
(3)寻找中序数组位置作为切割点;
(4)切中序数组;
(5)切后序数组;
(6)递归处理左区间、右区间。
2.分割区间时,一定要注意区间的形式。确定是左闭右开还是左闭右闭。
class Solution {
public:
TreeNode* traversal(vector<int>& inorder,vector<int>& postorder){
if(postorder.size() == 0) return NULL;
int rootvalue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootvalue);
if(postorder.size() == 1) return root;
int index;
for(index = 0; index < inorder.size(); index++){
if(inorder[index] == rootvalue)
break;
}
vector<int> leftInorder(inorder.begin(), inorder.begin() + index);
vector<int> rightInorder(inorder.begin() + index + 1, inorder.end());
postorder.resize(postorder.size() - 1);
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root -> left = traversal(leftInorder,leftPostorder);
root -> right = traversal(rightInorder,rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder,postorder);
}
};
3.从前序和中序遍历序列构造二叉树
4.nums.end()
返回一个指向容器末尾的迭代器,指向的位置是容器最后一个元素的下一个位置,通常被称为“尾后迭代器”。
对于数组 nums = {1, 2, 3, 4, 5}
,nums.end()
将返回一个指向数组末尾下一个位置的迭代器。因为数组有5个元素,所以这个迭代器将指向数组末尾的下一个位置,即超出数组范围。
因此,如果将 nums.end()
输出,你会得到一个指向数组末尾下一个位置的指针,这个指针通常不指向任何有效的元素,其行为通常是未定义的,因此不应该访问这个位置的内容。