二叉树刷题篇
Posted 归宅观察部
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树刷题篇相关的知识,希望对你有一定的参考价值。
404.左叶子之和
计算给定二叉树的所有左叶子之和。示例:
3
/
9 20
/
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
https://leetcode-cn.com/problems/sum-of-left-leaves/
class Solution {
public:
void traversal(TreeNode* cur,int direction,int &sum){
if(direction==0){
if(cur==NULL){
return;
}
if(cur->left==NULL&&cur->right==NULL){
sum+=cur->val;
return;
}
traversal(cur->left,0,sum);
traversal(cur->right,1,sum);
}
if(direction==1){
if(cur==NULL){
return;
}
traversal(cur->left,0,sum);
traversal(cur->right,1,sum);
}
}
int sumOfLeftLeaves(TreeNode* root) {
int direction;
int sum=0;
if(root==NULL){
return 0;
}
TreeNode* cur=root;
traversal(root->left,0,sum);
traversal(root->right,1,sum);
return sum;
}
};
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;
int leftValue = sumOfLeftLeaves(root->left); // 左
int rightValue = sumOfLeftLeaves(root->right); // 右
// 中
int midValue = 0;
if (root->left && !root->left->left && !root->left->right) { // 中
midValue = root->left->val;
}
int sum = midValue + leftValue + rightValue;
return sum;
}
};
//代码来自公众号代码随想录 作者为Carl
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
stack<TreeNode*> st;
if (root == NULL) return 0;
st.push(root);
int result = 0;
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) {
result += node->left->val;
}
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return result;
}
};
106. 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。注意: 你可以假设树中没有重复的元素。
例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树:
3
/
9 20
/
15 7
https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
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 delimiterIndex;
for(delimiterIndex=0;delimiterIndex<postorder.size();delimiterIndex++){
if(inorder[delimiterIndex]==rootValue) break;
}
/* vector<int> leftInorder,rightInorder;
* //注意这里的参数二为inorder.begin() + delimiterIndex。不需要再加1。
* leftInorder=inorder(inorder.begin(),inorder.begin()+delimiterIndex);
* rightInorder=inorder(inorder.begin()+delimiterIndex+1,inorder.end());
* vector<int> leftPostorder,rightPostorder;
*leftPostorder=postorder(postorder.begin(),postorder.begin()+leftInorder.size());
*rightPostorder=postorder(postorder.begin()+leftInorder.size(),postorder.end()-1);
*/
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end()-1);
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);
}
};
class Solution {
private:
// 中序区间:[inorderBegin, inorderEnd),后序区间[postorderBegin, postorderEnd)
TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& postorder, int postorderBegin, int postorderEnd) {
if (postorderBegin == postorderEnd) return NULL;
int rootValue = postorder[postorderEnd - 1];
TreeNode* root = new TreeNode(rootValue);
if (postorderEnd - postorderBegin == 1) return root;
int delimiterIndex;
for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序数组
// 左中序区间,左闭右开[leftInorderBegin, leftInorderEnd)
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;
// 右中序区间,左闭右开[rightInorderBegin, rightInorderEnd)
int rightInorderBegin = delimiterIndex + 1;
int rightInorderEnd = inorderEnd;
// 切割后序数组
// 左后序区间,左闭右开[leftPostorderBegin, leftPostorderEnd)
int leftPostorderBegin = postorderBegin;
int leftPostorderEnd = postorderBegin + delimiterIndex - inorderBegin; // 终止位置是 需要加上 中序区间的大小size
// 右后序区间,左闭右开[rightPostorderBegin, rightPostorderEnd)
int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);
int rightPostorderEnd = postorderEnd - 1; // 排除最后一个元素,已经作为节点了
root->left = traversal(inorder, leftInorderBegin, leftInorderEnd, postorder, leftPostorderBegin, leftPostorderEnd);
root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
// 左闭右开的原则
return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};
以上是关于二叉树刷题篇的主要内容,如果未能解决你的问题,请参考以下文章