力扣 110.平衡二叉树
题目链接:513. 找树左下角的值 - 力扣(LeetCode)
思路:1.明确递归函数参数和返回值,depth记录当前深度
2.本题没有中的处理逻辑,无论哪种情况,都会优先处理左,终止条件是遍历到叶子节点
3.在找最大深度的时候,递归的过程中依然要使用回溯
C++代码:
class Solution {
public:
int maxDepth=INT_MIN;
int result=0;
void traversal(TreeNode* node,int depth){
if(node->left==nullptr && node->right==nullptr){
if(depth>maxDepth){
maxDepth=depth;
result=node->val;
}
}
//等效于 traversal(node->left,depth+1)
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;
}
};
力扣 112.路径总和
题目链接:112. 路径总和 - 力扣(LeetCode)
思路:只要找到符合条件的一条路径,立刻返回。不需要遍历所有节点,所以返回值是bool类型。
1.明确递归函数参数和返回值,count记录总和,每遍历一个节点,count值减少
2.当处理到叶子节点,且count值为0,找到了一条符合条件的路径就返回。
C++代码:
class Solution {
public:
bool traversal(TreeNode* node,int count){
if(node->left==nullptr && node->right==nullptr && count==0) return true;
if(node->left==nullptr && node->right==nullptr && 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==nullptr) return false;
return traversal(root,targetSum-root->val);
}
};
力扣 113.路径总和2
题目链接:113. 路径总和 II - 力扣(LeetCode)
思路:由于要找到所有符合条件的路径,函数返回值应该为空
C++代码:
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){
path.push_back(node->left->val);
count-=node->left->val;
traversal(node->left,count);
count+=node->left->val;
path.pop_back(); //回溯
}
if(node->right){
path.push_back(node->right->val);
count-=node->right->val;
traversal(node->right,count);
count+=node->right->val;
path.pop_back(); //回溯
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
path.clear();
result.clear();
if(root==nullptr) return result;
path.push_back(root->val);
traversal(root,targetSum-root->val);
return result;
}
};
力扣 106.从中序与后序遍历序列构造二叉树
题目链接:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
思路:
-
第一步:如果数组大小为零的话,说明是空节点了。
-
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
-
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
-
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
-
第五步:切割后序数组,切成后序左数组和后序右数组
-
第六步:递归处理左区间和右区间
C++代码:
class Solution {
private:
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 i;
for( i=0;i<inorder.size();i++){
if(inorder[i]==rootValue){
break;
}
}
//切割中序数组
vector<int> inorderLeft(inorder.begin(),inorder.begin()+i);
vector<int> inorderRight(inorder.begin()+i+1,inorder.end());
postorder.resize(postorder.size()-1);
//切割后序数组
vector<int> leftPostorder(postorder.begin(), postorder.begin()+inorderLeft.size());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + inorderLeft.size(), postorder.end());
root->left=traversal(inorderLeft,leftPostorder);
root->right=traversal(inorderRight,rightPostorder);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==0 || postorder.size()==0) return nullptr;
return traversal(inorder,postorder);
}
};