代码随想录算法训练营第十六天 | 513.找树左下角的值 , 112. 路径总和,113. 路径总和ii,106.从中序与后序遍历序列构造二叉树 & 105.从前序与中序遍历序列构造二叉树
天呐疯狂赶进度,博客先一切从简,二刷再补充了
513. Find Bottom Left Tree Value
深搜用回溯
用层序很intuitive
题目链接:513. Find Bottom Left Tree Value
题目链接/文章讲解/视频讲解:代码随想录
我的代码:
Python(层序):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root:
return None
queue = deque([root])
result = 0
while queue:
size = len(queue)
for i in range(size):
node = queue.popleft()
if i == 0:
result = node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
Python(深搜):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root:
return None
queue = deque([root])
level = []
while queue:
level = []
size = len(queue)
for _ in range(size):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return level[0]
C++(深搜):
/**
* 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 maxDepth = INT_MIN;
int result;
void traversal(TreeNode*node, int depth) {
if (!node->left && !node->right) {
if (depth > maxDepth) {
maxDepth = depth;
result = node->val;
}
return;
}
if (node->left) {
depth++;
traversal(node->left, depth);
depth--;
}
if (node->right) {
depth++;
traversal(node->right, depth);
depth--;
}
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return result;
}
};
112. Path Sum
想到把target逐步减去,最后等于0说明找到目标路径,这个思路很绝
题目链接:112. Path Sum
题目链接/文章讲解/视频讲解:代码随想录112&113
113. Path Sum II
和112基本一样,只需要想出如何把每一条路径记录下来
题目链接:113. Path Sum II
题目链接/文章讲解/视频讲解:代码随想录112&113
我的代码:
C++:
/**
* 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 {
private:
vector<vector<int>> result;
vector<int> cur;
public:
void traversal(TreeNode* node, int count) {
if (!node->left && !node->right && count==0) {
result.push_back(cur);
}
if (!node->left && !node->right) {
return;
}
if (node->left) {
count -= node->left->val;
cur.push_back(node->left->val);
traversal(node->left, count);
count += node->left->val;
cur.pop_back();
}
if (node->right) {
count -= node->right->val;
cur.push_back(node->right->val);
traversal(node->right, count);
count += node->right->val;
cur.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if (root == NULL) {
return result;
}
cur.push_back(root->val);
traversal(root, targetSum - root->val);
return result;
}
};
106. Construct Binary Tree from Inorder and Postorder Traversal
关键是先切割中序,再切割后序
题目链接:106. Construct Binary Tree from Inorder and Postorder Traversal
题目链接/文章讲解/视频讲解:代码随想录105&106
我的代码:
C++:
/**
* 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 {
private:
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 middle;
for (middle=0; middle < inorder.size(); middle++) {
if (inorder[middle] == rootValue) {
break;
}
}
vector<int> leftInorder(inorder.begin(), inorder.begin()+middle);
vector<int> rightInorder(inorder.begin()+middle+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;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) {
return NULL;
}
return traversal(inorder, postorder);
}
};
105. Construct Binary Tree from Preorder and Inorder Traversal
和106思路一样
题目链接:105. Construct Binary Tree from Preorder and Inorder Traversal
题目链接/文章讲解/视频讲解:代码随想录105&106
我的代码:
C++:
/**
* 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 {
private:
TreeNode* traversal(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0) {
return NULL;
}
int rootVal = preorder[0];
TreeNode* root = new TreeNode(rootVal);
if (preorder.size() == 1) {
return root;
}
int middle;
for (middle=0; middle < inorder.size(); middle++) {
if (inorder[middle] == rootVal) {
break;
}
}
vector<int> leftInorder(inorder.begin(), inorder.begin()+middle);
vector<int> rightInorder(inorder.begin()+middle+1, inorder.end());
vector<int> leftPreorder(preorder.begin()+1, preorder.begin()+1+leftInorder.size());
vector<int> rightPreorder(preorder.begin()+1+leftInorder.size(), preorder.end());
root->left = traversal(leftPreorder, leftInorder);
root->right = traversal(rightPreorder, rightInorder);
return root;
}
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0 || inorder.size() == 0) {
return NULL;
}
return traversal(preorder, inorder);
}
};
总结
救命,前段时间research很忙,现在疯狂赶进度