代码随想录刷题第18天
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 min_depth = -1;
int result;
void transation(TreeNode* root,int depth){
if(!root->left && !root->right){
if(depth > min_depth){
min_depth = depth;
result = root->val;
}
return;
}
if(root->left){
depth++;
transation(root->left, depth);
depth--;
}
if(root->right){
depth++;
transation(root->right, depth);
depth--;
}
}
int findBottomLeftValue(TreeNode* root) {
transation(root, 0);
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:
// vector<int> path;
void transation(TreeNode* root, int targetSum,int &result){
// path.push_back(root->val);
int tmp = targetSum - root->val;
if(!root->left && !root->right){
//叶子节点
if(tmp == 0){
result = 1;
}
return;
}
if(root->left){
int tmp1 = targetSum - root->val;
transation(root->left, tmp1,result);
}
if(root->right){
int tmp1 = targetSum - root->val;
transation(root->right, tmp1,result);
}
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == nullptr) {
return false;
}
int result = -1;
transation(root, targetSum,result);
return result == -1 ? false : true;
}
};
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* transation(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());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = transation(leftInorder, leftPostorder);
root->right = transation(rightInorder, rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
TreeNode *a = transation(inorder, postorder);
return a;
}
};