513找左下角的值
题目链接:找左下角的值
题目描述:
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点
解题思路:
- 碰到该题的第一个想法就是通过层序遍历,当遍历到最后一层时,我们只需要记住第一个数字就是左下角的值。
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* cur=que.front();
que.pop();
if(i==0) result=cur->val;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return result;
}
};
递归法
同样的,我们需要知道最后一层在哪里,然后找到第一个节点。因此,我们采用前序遍历。
class Solution {
public:
int result;
int maxDepth=INT_MIN;
int findBottomLeftValue(TreeNode* root) {
traversal(root,0);
return result;
}
void traversal(TreeNode* cur,int depth)
{
if(cur->left==NULL&&cur->right==NULL)
{
if(maxDepth<depth)
{
maxDepth=depth;
result=cur->val;
}
return ;
}
if(cur->left) traversal(cur->left,depth+1);
if(cur->right) traversal(cur->right,depth+1);
return ;
}
};
112 路径总和
题目链接:112路径总和
题目描述:
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
解题思路:
1 该题其实与前两天有个输出所有路径一样,这次只是将路径加起来就行。因此,本题同样采用递归法。
2. 当然本题也可以采用迭代法,由于本身我们并不需要对节点做什么操作,因此,采用什么样的遍历方式都是可以的。
递归法
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==NULL) return false;
return traversal(root,root->val,targetSum);
}
bool traversal(TreeNode* cur,int sum,int targetSum)
{
// sum+=cur->val;
if(cur->left==NULL&&cur->right==NULL&&sum==targetSum)
{
return true;
}
if(cur->left==NULL&&cur->right==NULL) return false;
if(cur->left)
{
sum+=cur->left->val;
if(traversal(cur->left,sum,targetSum)) return true;
sum-=cur->left->val;
}
if(cur->right)
{
sum+=cur->right->val;
if(traversal(cur->right,sum,targetSum)) return true;
sum-=cur->right->val;
}
return false;
}
};
迭代法
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==NULL) return false;
stack<pair<TreeNode*,int>>st;
st.push(pair<TreeNode*,int>(root,root->val));
while(!st.empty())
{
pair<TreeNode*,int>cur=st.top();
st.pop();
if(cur.first->left==NULL&&cur.first->right==NULL&&cur.second==targetSum) return true;
if(cur.first->left)
st.push(pair<TreeNode*,int>(cur.first->left,cur.second+cur.first->left->val));
if(cur.first->right)
st.push(pair<TreeNode*,int>(cur.first->right,cur.second+cur.first->right->val));
}
return false;
}
};
113 路径总和II
题目链接:113路径总和II
题目描述:
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点
解题思路:
本题跟上一题是一样的,上一题是返回true或者false,然而本题返回的是所有的路径。同样采用两种算法,一种迭代,一种递归。首先递归法。
递归法
class Solution {
public:
vector<vector<int>>result;
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(root==NULL) return result;
vector<int>path;
path.push_back(root->val);
traversal(root,path,root->val,targetSum);
return result;
}
void traversal(TreeNode* cur,vector<int>path,int sum,int targetSum)
{
if(!cur->left&&!cur->right&&sum==targetSum)
{
result.push_back(path);
return;
}
if(!cur->left&&!cur->right) return;
if(cur->left)
{
path.push_back(cur->left->val);
sum+=cur->left->val;
traversal(cur->left,path,sum,targetSum);
path.pop_back();
sum-=cur->left->val;
}
if(cur->right)
{
path.push_back(cur->right->val);
sum+=cur->right->val;
traversal(cur->right,path,sum,targetSum);
path.pop_back();
sum-=cur->right->val;
}
}
};
106 从中序和后序遍历序列构造二叉树
题目链接:106从中序和后序遍历序列构造二叉树
题目描述:给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
解题思路:
- 首先我们需要知道,构造二叉树必须要存在中序序列,因此,前序和后序遍历序列是无法构造二叉树的。
- 我们一般解题时,基本上首先从后序遍历中寻找根元素,因此,在代码编写时同样如此,根据后序遍历的最后一个元素的值,然后从中序遍历中找到相应的位置,然后该位置的左边就是左子树,右边就是右子树,进行划分,然后左子树构造,右子树构造。
- 同时,我们需要注意的一点是,如何根据中序遍历划分好的左右子树,在后序遍历中寻找,!!!根据的是节点的个数。左子树和右子树的个数是一定的,作为后序遍历,同样先是左子树然后右子树,我们首先在中序遍历中确定好左子树中节点的个数,然后根据个数在后序遍历中找到划分位置。
代码如下:
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==0||postorder.size()==0) return NULL;
return traversal(inorder,postorder,0,inorder.size(),0,postorder.size());
}
TreeNode* traversal(vector<int>& inorder,vector<int>&postorder,int inbegin,int inend,int postbegin,int postend)
{
if(postbegin==postend) return NULL;
int rootValue=postorder[postend-1];//找到根节点的值,然后在中序遍历中找到相应的位置
TreeNode* root=new TreeNode(rootValue);
int midIndex;
for(midIndex=inbegin;midIndex<inend;midIndex++)
{
if(inorder[midIndex]==rootValue) break;
}
int leftInBegin=inbegin;
int leftInEnd=midIndex;
int rightInBegin=midIndex+1;
int rightInEnd=inend;
int leftPostBegin=postbegin;
int leftPostEnd=postbegin+midIndex-inbegin;
int rightPostBegin=postbegin+midIndex-inbegin;
int rightPostEnd=postend-1;
root->left=traversal(inorder,postorder,leftInBegin,leftInEnd,leftPostBegin,leftPostEnd);
root->right=traversal(inorder,postorder,rightInBegin,rightInEnd,rightPostBegin,rightPostEnd);
return root;
}
};
105 从前序和后序遍历序列构造二叉树
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size()==0||inorder.size()==0) return NULL;
return traversal(preorder,inorder,0,inorder.size(),0,preorder.size());
}
TreeNode* traversal(vector<int>& preorder,vector<int>&inorder,int inbegin,int inend,int prebegin,int preend){
if(prebegin==preend) return NULL;
int rootValue=preorder[prebegin];
TreeNode* root=new TreeNode(rootValue);
int midIndex;
for(midIndex=inbegin;midIndex<inend;midIndex++)
{
if(inorder[midIndex]==rootValue) break;
}
int leftInbegin=inbegin;
int leftInend=midIndex;
int rightInbegin=midIndex+1;
int rightInend=inend;
int leftPrebegin=prebegin+1;
int leftPreend=prebegin+1+midIndex-inbegin;
int rightPrebegin=prebegin+1+midIndex-inbegin;
int rightPreend=preend;
root->left=traversal(preorder,inorder,leftInbegin,leftInend,leftPrebegin,leftPreend);
root->right=traversal(preorder,inorder,rightInbegin,rightInend,rightPrebegin,rightPreend);
return root;
}
};