当前位置:网站首页>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;
}
};

边栏推荐
- Qt使用Valgrind分析内存泄漏
- [cocos creator 3.5.1] addition of coordinates
- 代码即数据
- Senior [Software Test Engineer] learning route and necessary knowledge points
- Change the status to the corresponding text during MySQL query
- C# netcore中 配置帮助类IConfiguration
- 【Cocos Creator 3.5.1】input.on的使用
- 【QT小点】QT下载链接
- 【QT小点】实现看门狗功能,检测外部程序是否在运行
- 【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
猜你喜欢
随机推荐
Assembly language - Wang Shuang Chapter 3 notes and experiments
开门小例子学习十种用例图
汇编语言-王爽 第9章 转移指令的原理-笔记
资深【软件测试工程师】学习线路和必备知识点
JVM对象组成和存储
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
IAR Systems全面支持芯驰科技9系列芯片
QListWidget中的内容不显示
Comprehensive application of OpenCV in contour detection and threshold processing
双位置继电器JDP-1440/DC110V
力扣 179、最大数
Unicast, multicast and broadcast of IP network communication
310. 最小高度树
多线程基础部分Part 1
【入门】正则表达式基础入门笔记
Asp.Net Core6 WebSocket 简单案例
【Cocos Creator 3.5.1】坐标的加法
Leetcode298 weekly race record
Multithreading basic part part 1
Software testing year end summary report template









