# 【代码随想录算法训练营第37期 第十八天 | LeetCode513.找树左下角的值、112. 路径总和、113.路径总和ii、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构】

## 一、513.找树左下角的值

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
if(root) 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;
}
};


## 二、112. 路径总和

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool traversal(TreeNode* cur, int count)
{
if(cur->left == NULL && cur->right == NULL && count == 0) return true;
if(cur->left == NULL && cur->right == NULL) return false;

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;
}

bool hasPathSum(TreeNode* root, int targetSum) {
if(!root) return false;
return traversal(root, targetSum - root->val);
}
};


## 三、113.路径总和ii

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> result;
vector<int> path;

void traversal(TreeNode* cur, int count)
{
if(cur->left == NULL && cur->right == NULL && count == 0)
{
result.push_back(path);
return;
}

if(cur->left == NULL && cur->right == NULL) return;

if(cur->left)
{
count -= cur->left->val;
path.push_back(cur->left->val);
traversal(cur->left, count);
path.pop_back();
count += cur->left->val;
}

if(cur->right)
{
count -= cur->right->val;
path.push_back(cur->right->val);
traversal(cur->right, count);
path.pop_back();
count += cur->right->val;
}

return;
}

vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
result.clear();
path.clear();

if(!root) return result;
path.push_back(root->val);
traversal(root, targetSum -= root->val);

return result;
}
};


## 四、106.从中序与后序遍历序列构造二叉树

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
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 < inorder.size(); delimiterIndex ++)
if(inorder[delimiterIndex] == rootValue)
break;

//切割中序遍历数组 (左闭右开)
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
vector<int> rightInorder(inorder.begin() + delimiterIndex + 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.从前序与中序遍历序列构造二叉树

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* traversal(vector<int>& preorder, int preorderBegin, int preorderEnd, vector<int>& inorder, int inorderBegin, int inorderEnd)
{
if(preorderBegin == preorderEnd) return NULL;

int rootValue = preorder[preorderBegin];
TreeNode* root = new TreeNode(rootValue);

if(preorderEnd - preorderBegin == 1) return root;

int delimiterIndex;
for(delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex ++)
if(inorder[delimiterIndex] == rootValue)
break;

//分割中序遍历，区间左闭右开
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;

int rightInorderBegin = delimiterIndex + 1;
int rightInorderEnd = inorderEnd;

//分割前序遍历
int leftPreorderBegin = preorderBegin + 1;
int leftPreorderEnd = preorderBegin + 1 + delimiterIndex - leftInorderBegin;

int rightPreorderBegin = preorderBegin + 1 + delimiterIndex - leftInorderBegin;
int rightPreorderEnd = preorderEnd;

root->left = traversal(preorder, leftPreorderBegin, leftPreorderEnd, inorder, leftInorderBegin, leftInorderEnd);
root->right = traversal(preorder, rightPreorderBegin, rightPreorderEnd, inorder, rightInorderBegin, rightInorderEnd);

return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size() == 0 || inorder.size() == 0) return NULL;
return traversal(preorder, 0, preorder.size(), inorder, 0, inorder.size());
}
};

• 5
点赞
• 4
收藏
觉得还不错? 一键收藏
• 1
评论

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。