Leetcode刷题第20天
一、找树左下角的值
题目:513. Find Bottom Left Tree Value
C++ Soution 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root)
{
queue<TreeNode*> q;
q.push(root);
TreeNode* curr;
while (!q.empty())
{
curr = q.front();
q.pop();
if (curr->right != NULL) q.push(curr->right);
if (curr->left != NULL) q.push(curr->left);
}
return curr->val;
}
};
C++ Soution 2:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
int findBottomLeftValue(TreeNode* root)
{
, res = root->val;
helper(root, , max_depth, res);
return res;
}
void helper(TreeNode* node, int depth, int& max_depth, int& res)
{
if (!node) return;
if (depth > max_depth)
{
max_depth = depth;
res = node->val;
}
helper(node->left, depth + , max_depth, res);
helper(node->right, depth + , max_depth, res);
}
};

二、二叉树的层次遍历
题目:102. Binary Tree Level Order Traversal
C++ Soution 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
if (!root) return {};
vector<vector<int>> res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
int count = q.size();
vector<int> temp;
while (count--)
{
TreeNode* Qu = q.front();
q.pop();
if (Qu->left) q.push(Qu->left);
if (Qu->right) q.push(Qu->right);
temp.push_back(Qu->val);
}
res.push_back(temp);
}
return res;
}
};
三、最大二叉树
/*
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
class Solution
{
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums)
{
)
return NULL;
else
{
;
; i < nums.size(); i++)
{
if (maxNum < nums[i])
{
maxNum = nums[i];
index = i;
}
}
vector<int> left(nums.begin(), nums.begin() + index);
vector<, nums.end());
TreeNode* root = new TreeNode(maxNum);
root->left = constructMaximumBinaryTree(left);
root->right = constructMaximumBinaryTree(right);
return root;
}
}
};
四、相同的树
C++ Soution 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q)
{
if(!p && !q) return true;
if(!p || !q) return false;
if(p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
五、二叉树的垂直遍历
题目:987. Vertical Order Traversal of a Binary Tree
C++ Soution 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root)
{
map<int, vector<int> > m;
queue<pair<int, TreeNode*> > q;
q.push(make_pair(, root));
while (!q.empty())
{
set<pair<int, int> > tmp;
int len = q.size();
; i < len; ++i)
{
auto p = q.front(); q.pop();
tmp.insert(make_pair(p.first, p.second->val));
, p.second->left));
, p.second->right));
}
for (auto p : tmp) m[p.first].push_back(p.second);
}
vector<vector<int> > res;
for (auto kv : m) res.push_back(kv.second);
return res;
}
};

Leetcode刷题第20天的更多相关文章
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- C#LeetCode刷题-栈
栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水 35.6% 困难 71 简 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题-双指针
双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.5% 中等 11 盛最多水的容器 43.5% 中等 15 三数之和 16.1% 中等 16 最接近的三数之和 3 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
随机推荐
- [Kubernetes]如何使用yaml文件使得可以向外暴露服务
最近因为项目需要上线,所以这段时间都扑到了Kubernetes上面. 昨天老大交代了一个任务,大概就是这样的: 看起来挺简单的,因为相关文件都给我了,我以为直接把文件拖上去,然后在访问ip:port方 ...
- 查找轮廓(cv2.findCountours函数)
1.输入为二值图像,黑色为背景,白色为目标 2.该函数会修改原图像,因此若想保留原图像在,则需拷贝一份,在拷贝图里修改. 一.查找轮廓 cv2.findContours() 三个输入参数:输入图像(二 ...
- mysql忘记root密码,修改mysql密码
1.修改mysql配置文件 vim /etc/my.cnf #编辑文件 找到[mysqld],在下面添加一行 skip-grant :wq #保存退出 service mysqld restart ...
- LwIP Application Developers Manual12---Configuring lwIP
1.前言 2.LwIP makefiles With minimal features C_SOURCES = \ src/api/err.c \ src/core/init.c \ src/core ...
- Nginx在线服务状态下平滑升级及ab压力测试【转】
今天,产品那边发来需求,说有个 APP 的 IOS 版本下载包需要新增 https 协议,在景安购买了免费的 SSL 证书.当我往 nginx 上新增 ssl 时,发现服务器上的 nginx 居然没编 ...
- lzstring
import lzstring ic = {"name": "root", "password": "123456"} ...
- EF 常见异常总结
问题:System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invoca ...
- Cinder模块学习
理解 Cinder 架构 - 每天5分钟玩转 OpenStack(45) 从本节开始我们学习 OpenStack 的 Block Storage Service,Cinder 理解 Block S ...
- node.js总结
1.NPM安装报错:no such file or directory, open 'C:\Users\HP\package.json' npm WARN saveError ENOENT: no s ...
- [转载]RabbitMQ消息可靠性分析
有很多人问过我这么一类问题:RabbitMQ如何确保消息可靠?很多时候,笔者的回答都是:说来话长的事情何来长话短说.的确,要确保消息可靠不只是单单几句就能够叙述明白的,包括Kafka也是如此.可靠并不 ...