题目分析:在树的最后一行找到最左边的值。
1、如何判断是最后一行?深度最大的叶子结点一定是最后一行 --找深度最大的叶子结点。
2、如何找最左边?可以使用前序遍历(中序、后序也可以,因为本题没有中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,如何记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
递归法
1.确定递归函数的参数和返回值
参数:遍历的树的根节点,一个int型变量用来记录最大深度。
返回值:不需要,void类型。
int maxDepth = INT_MIN; // 全局变量 记录最大深度
int result; // 全局变量 最大深度最左节点的数值
void traversal(TreeNode* root, int depth)
2.确定终止条件
当遇到叶子节点的时候,需要统计最大的深度。
if (root->left == NULL && root->right == NULL) {
if (depth > maxDepth) {
maxDepth = depth; // 更新最大深度
result = root->val; // 最大深度最左面的数值
}
return;
}
3.确定单层递归逻辑
在找最大深度的时候,递归的过程依然要使用回溯。
// 中
if (root->left) { // 左
depth++; // 深度加一
traversal(root->left, depth);
depth--; // 回溯,深度减一
}
if (root->right) { // 右
depth++; // 深度加一
traversal(root->right, depth);
depth--; // 回溯,深度减一
}
return;
总体实现如下:
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode*node, int depth){
if(node->left==NULL&&node->right==NULL){
if(depth>maxDepth){
maxDepth = depth;
result = node->val;
}
}
if(node->left){
depth++;
traversal(node->left,depth);
depth--;
}
if(node->right){
depth++;
traversal(node->right,depth);
depth--;
}
}
int findBottomLeftValue(TreeNode* root) {
traversal(root,0);
return result;
}
};
迭代法
记录最后一行的第一个节点即可!
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
int result = 0;
while(!que.empty()){
int size = que.size();
for(int i =0;i<size;i++){
TreeNode* node = que.front();
que.pop();
if(i==0) result = node->val;//每次都记录此层的第一个节点,最后记录的是最后一层第一个节点
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return result;
}
};
递归法
可以使用深度优先遍历法方式(本题前、中、后序都可以)来遍历二叉树
1.确定递归函数的参数和返回类型
参数:需要二叉树的根节点,还需要一个计数器,这个计数器用来计算二叉树的一条边之和是否正好是目标和,计数器为int。
递归函数何时需要返回值?什么时候不需要返回值?
1)如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值;
2)如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值;
3)如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合路径了就要及时返回。(本题)
bool traversal(treenode* cur, int count) // 注意函数的返回类型
2.确定终止条件
计数器统计一条路径上的和?不要去累加然后判断是否等于目标和, 可以用递减,让计数器count初始为目标和,然后每次减去遍历路径节点上的数值。如果最后count==0,同时找到了叶子节点的话,说明找到了目标和。如果遍历到了叶子结点,count不等于0,就是没找到。
if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
if (!cur->left && !cur->right) return false; // 遇到叶子节点而没有找到合适的边,直接返回
3.确定单层递归逻辑
因为终止条件是判断叶子节点,所以递归的过程中不要让空节点进入递归了。
递归函数是有返回值的,如果递归函数返回true吗,说明找到了合适的路径,应该立即返回。
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;
整体代码如下:
class Solution {
public:
bool traversal(TreeNode* node, int count){
if(!node->left&&!node->right&&count==0) return true;
if(!node->left&&!node->right&&count!=0) return false;
if(node->left){
count-=node->left->val;
if(traversal(node->left, count)) return true;
count+=node->left->val;
}
if(node->right){
count-=node->right->val;
if(traversal(node->right, count)) return true;
count+=node->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==NULL) return false;
return traversal(root, targetSum-root->val);
}
};
此题需要遍历整个树,找到所有的路径,所以递归函数不要返回值。
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void traversal(TreeNode* node, int count){
if(!node->left&&!node->right&&count==0){
result.push_back(path);
return;
}
if(!node->left&&!node->right) return;
if(node->left){
count-=node->left->val;
path.push_back(node->left->val);
traversal(node->left,count);
count+=node->left->val;
path.pop_back();
}
if(node->right){
count-=node->right->val;
path.push_back(node->right->val);
traversal(node->right,count);
count+=node->right->val;
path.pop_back();
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
result.clear();
path.clear();
if(root==NULL) return result;
path.push_back(root->val);
traversal(root,targetSum-root->val);
return result;
}
};
106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
一些分析:
例1:已知一棵二叉树的前序遍历为ABCDEF,中序遍历序列为CBAEDF,请问这棵二叉树的后序遍历结果是多少?
三种遍历都是从根节点开始,前序遍历是先打印再递归左和右。所以前序遍历序列ABCDEF,第一个字母是A被打印出来,就说明A是根节点的数据。再由中序遍历序列CBAEDF,可以知道C和B是A的左子树的节点,E、D、F是A的右子树的节点。
然后我们看前序中的C和B,他的顺序是ABCDEF,是先打印B再打印C,所以B应该是A的左孩子,而C只能是B的孩子,此时是左孩子还是右孩子还不确定。再看中序序列CBAEDF,C在B前面打印,这就说明了C是B的左孩子,否则就是右孩子了。
再看前序中的E、D、F,它的顺序是ABCDEF,那就意味着D是A节点的右孩子,E和F是D的子孙。再来看中序序列CBAEDF,由于E在D的左侧,而F在右侧,所以可以确定E是D的左孩子,F是D的右孩子。
例2:二叉树的中序序列是ABCDEFG,后序序列是BDCAFGE,求前序序列。
由后序序列BDCAFGE,得到E是根节点,因此前序首字母是E。于是根据中序序列分成两棵树ABCD和FG,由后序序列BDCAFGE,A是E的左孩子,前序序列目前分析为EA。
再由中序序列的ABCDEFG,知道BCD是A节点的右子孙,再由后序序列的BDCAFGE知道C节点是A节点的右孩子,前序序列目前分析得到EAC。
由中序序列ABCDEFG得到,B是C的左孩子,D是C的右孩子,所以前序序列目前分析为EACBD。
由后序序列BDCAFGE,得到G是E的右孩子,所以前序序列目前分析为EACBDG。
由中序序列ABCDEFG,可以得出F是G的左孩子,前序序列为EACBDGF。
总结:
1)如果数组大小为0,说明是空节点;
2)如果不为空,那么取后序数组最后一个元素作为节点元素;
3)找到后序数组最后一个元素在中序数组的位置,作为切割点;
4)切割中序数组,切成中序左数组和中序右数组;
5)切割后序数组,切成后序左数组和后序右数组;
6)递归处理左区间和右区间
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 =0;
for(index=0;index<inorder.size();index++){
if(inorder[index]==rootvalue) break;
}
//切割中序数组
//左闭右开区间:[0,index)
vector<int> leftinorder(inorder.begin(),inorder.begin()+index);
//[index+1,end)
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);
}
};
105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)
class Solution {
public:
TreeNode* traversal(vector<int>& inorder,int inorderBegin,int inorderEnd, vector<int>& preorder,int preorderBegin,int preorderEnd){
if(preorderBegin==preorderEnd) return NULL;
int rootvalue = preorder[preorderBegin];//注意用preorderBegin 不要用0
TreeNode* root = new TreeNode(rootvalue);
if(preorderEnd-preorderBegin==1) return root;
int index;
for(index=0;index<inorder.size();index++){
if(inorder[index]==rootvalue) break;
}
//切割中序数组
//中序左区间,左闭右开
int leftInorderBegin = inorderBegin;
int leftInorderEnd = index;
//中序右区间,左闭右开
int rightInorderBegin = index+1;
int rightInorderEnd = inorderEnd;
//切割前序数组
int leftPreorderBegin = preorderBegin+1;
int leftPreorderEnd = preorderBegin+1+index-inorderBegin;//终止位置是起始位置加上中序左区间大小的size
int rightPreorderBegin = preorderBegin+1+index-inorderBegin;
int rightPreorderEnd = preorderEnd;
root->left = traversal(inorder,leftInorderBegin,leftInorderEnd,preorder,leftPreorderBegin,leftPreorderEnd);
root->right = traversal(inorder,rightInorderBegin,rightInorderEnd,preorder,rightPreorderBegin,rightPreorderEnd);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(inorder.size()==0||preorder.size()==0) return NULL;
return traversal(inorder,0,inorder.size(),preorder,0,preorder.size());
}
};