当前位置:网站首页>426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
2022-06-27 06:01:00 【liufeng2023】
513. Find the value in the lower left corner of the tree

class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
if (root == nullptr) return 0;
int res = 0;
que.push(root);
while (!que.empty())
{
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (i == 0) res = node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return res;
}
};
112. The sum of the paths

class Solution {
public:
bool traversal(TreeNode* cur, int count)
{
if (cur->left == nullptr && cur->right == nullptr && count == 0) return true;
if (cur->left == nullptr && cur->right == nullptr) 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;
}
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr) return false;
return traversal(root, targetSum - root->val);
}
};
106. Construct binary tree from middle order and post order traversal sequence
654. The largest binary tree

class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* node = new TreeNode(0);
if (nums.size() == 1)
{
node->val = nums[0];
return node;
}
int maxValue = 0;
int maxValueIndex = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] > maxValue)
{
maxValue = nums[i];
maxValueIndex = i;
}
}
node->val = maxValue;
if (maxValueIndex > 0)
{
vector<int> newVec(nums.begin(), nums.begin() + maxValueIndex);
node->left = constructMaximumBinaryTree(newVec);
}
if (maxValueIndex < (nums.size() - 1))
{
vector<int> newVec(nums.begin() + maxValueIndex + 1, nums.end());
node->right = constructMaximumBinaryTree(newVec);
}
return node;
}
};

边栏推荐
猜你喜欢
随机推荐
块级元素&行内元素
使用 WordPress快速个人建站指南
Comprehensive application of OpenCV in contour detection and threshold processing
【QT小点】QT下载链接
【Cocos Creator 3.5.1】input. Use of on
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
Two position relay hjws-9440
Get system volume across platforms in unity
LeetCode-515. 在每个树行中找最大值
Openresty usage document
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
多线程基础部分Part3
Junda technology - centralized monitoring scheme for multi brand precision air conditioners
【Cocos Creator 3.5.1】event.getButton()的使用
MATLAB快速将影像的二维坐标转换为经纬度坐标
Configuring the help class iconfiguration in C # NETCORE
Senior [Software Test Engineer] learning route and necessary knowledge points
openresty使用文档
C语言实现定时器
Double position relay rxmd2-1mrk001984 dc220v

![Navigation [machine learning]](/img/79/8311a409113331e72f650a83351b46.png)






