513.找树左下角的值
二叉树均为分递归法和迭代法来解;
递归法:
第一步,确定递归参数与返回值,参数必须包括节点,还需要将层数信息包含进来,额外信息还需要目前最大层数,以及保存节点信息的value值
第二步,确定终止条件,当节点为叶子节点就可以结束了
第三步,确定单层逻辑,当前节点为叶子节点时,判断此层高度是否是当前遍历到的最大的,如果是,那么需要更新value与maxDepth,即使是前序遍历(中-左-右)、中序遍历(左-中-右)、后序遍历(左-右-中),很明显,每次都是从左边开始的,那么保证每次更新到的都是树最左边值;
class Solution {
public:
void travel(TreeNode* root,int &maxDepth,int depth,int& value) {
if(root->left==nullptr&&root->right==nullptr) {
if(depth>maxDepth) {
maxDepth=depth;
value=root->val;
}
}
if(root->left) {
travel(root->left,maxDepth,depth+1,value);
}
if(root->right) {
travel(root->right,maxDepth,depth+1,value);
}
}
int findBottomLeftValue(TreeNode* root) {
int value=0;
int maxDepth=0;
travel(root,maxDepth,1,value);
return value;
}
};
迭代法,就是层序遍历来看的;不再写了
112.路径总和
这题需要注意的点就是需要遍历到叶子节点的时候才能够做判断
递归法:
第一步,确定递归函数参数与返回值,参数需要节点和target值,需要返回boo值
第二步,确定函数终止条件,当为叶子节点需要考虑这条路径的和是否等于taget,如果不等于就返回false;
第三步,确定单层逻辑,分别判断左子树与右子树是否有满足条件的路径。
class Solution {
public:
bool travel(TreeNode* root,int target) {
if(root->left==nullptr&&root->right==nullptr) {
if(target==root->val) {
return true;
}
return false;
}
bool left=false,right=false;
if(root->left) {
left=travel(root->left,target-root->val);
}
if(root->right) {
right=travel(root->right,target-root->val);
}
return left|right;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr) return false;
return travel(root,targetSum);
}
};
113.路经总和ii
扩展题,将对应路径存起来
class Solution {
public:
void travel(TreeNode* root,vector<vector<int>>&result,vector<int> temp,int target) {
if(root->left==nullptr&&root->right==nullptr) {
if(target==root->val) {
temp.emplace_back(root->val);
result.emplace_back(temp);
}
}
if(root->left) {
temp.emplace_back(root->val);
travel(root->left,result,temp,target-root->val);
temp.pop_back();
}
if(root->right) {
temp.emplace_back(root->val);
travel(root->right,result,temp,target-root->val);
temp.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> result;
vector<int> temp;
if(root==nullptr) return result;
travel(root,result,temp,targetSum);
return result;
}
};
106.中序遍历与后序遍历构造二叉树
之前做过很多次的构造树,递归法最容易理解
第一步,判断函数参数与返回值,中序与后序遍历数组,还需要包括,对应数组的左右边界,返回值需要返回节点
第二步,判断终止条件,当左边界大于右边界时,说明数组没有数值可用了,返回
第三步,函数单层逻辑,每次找出对应边界内的根节点,然后分开左子树的范围与右子树的范围,再找到对应左子树的根节点与右子树的根节点;
class Solution {
public:
TreeNode* InOrderandPreOrder(vector<int>& inoder,vector<int>& postorder,int inl,int inr,int postl,int postr) {
if(inl>inr) return nullptr;
int t;
for(int i=inl;i<=inr;i++) {
if(postorder[postr]==inoder[i]) {
t=i;
break;
}
}
TreeNode *node=new TreeNode(postorder[postr]);
node->left=InOrderandPreOrder(inoder,postorder,inl,t-1,postl,postl+t-1-inl);
node->right=InOrderandPreOrder(inoder,postorder,t+1,inr,postl+t-inl,postr-1);
return node;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
TreeNode* root=InOrderandPreOrder(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
return root;
}
};
105.中序与前序遍历构造二叉树
类似上一题的做法
class Solution {
public:
TreeNode* preorderandInorder(vector<int>& preorder,vector<int>& inorder,int prel,int prer,int inl,int inr) {
if(prel>prer) return nullptr;
int t;
for(int i=inl;i<+inr;i++) {
if(preorder[prel]==inorder[i]) {
t=i;
break;
}
}
TreeNode* node=new TreeNode(preorder[prel]);
node->left=preorderandInorder(preorder,inorder,prel+1,prel+t-inl,inl,t-1);
node->right=preorderandInorder(preorder,inorder,prel+t-inl+1,prer,t+1,inr);
return node;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode* root=preorderandInorder(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
return root;
}
};