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

边栏推荐
- Double position relay rxmd2-1mrk001984 dc220v
- Contents in qlistwidget are not displayed
- QListWidgetItem上附加widget
- 1317. 将整数转换为两个无零整数的和
- IP网络通信的单播、组播和广播
- Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
- 微信小程序WebSocket使用案例
- 【入门】正则表达式基础入门笔记
- Built in functions of spark
- 树莓派4B上运行opcua协议DEMO接入kubeedge
猜你喜欢

双位置继电器JDP-1440/DC110V

资深【软件测试工程师】学习线路和必备知识点

IAR systems fully supports Centrino technology 9 series chips

JVM常用指令

竣达技术丨多品牌精密空调集中监控方案

JS to implement bidirectional data binding

Two position relay rxmvb2 r251 204 110dc

函数栈帧的形成与释放

Program ape learning Tiktok short video production

KubeSphere 集群配置 NFS 存储解决方案-收藏版
随机推荐
Double position relay jdp-1440/dc110v
310. 最小高度树
Jump details of item -h5 list, and realize the function of not refreshing when backing up, and refreshing when modifying data (record scroll bar)
洛谷P2939 [USACO09FEB]Revamping Trails G 题解
1317. convert an integer to the sum of two zero free integers
Built in functions of spark
leetcode299周赛记录
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design
G1和ZGC垃圾收集器
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi
【Cocos Creator 3.5.1】event. Use of getbutton()
Spark 之 Projection
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
30 SCM common problems and solutions!
Asp.Net Core6 WebSocket 简单案例
洛谷P4683 [IOI2008] Type Printer 题解
Spark 之 WholeStageCodegen
Wechat applet websocket use case
js实现双向数据绑定