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 字符串转整数 ...
随机推荐
- 2017-2018-2 20165231实验二《Java面向对象程序设计》实验报告
实验报告封面 课程:Java程序设计 班级:1652班 姓名:王杨鸿永 学号:20165231 指导教师:娄嘉鹏 实验日期:2018年4月16日 实验时间:13:45 - 15:25 实验序号:实验二 ...
- Linux网络底层收发探究【转】
转自:https://blog.csdn.net/davion_zhang/article/details/51536807 本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- PHP JSON 数据解析代码
作者: 字体:[增加减小] 类型:转载 PHP解析JSON 数据代码,与大多数流行的 Web 服务如 twitter .人人网通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据 ...
- kafka组件makemirror处理跨机房业务的应用
业务背景:app分散在不同的idc厂商不同的地域,产生业务数据都向一个kafka中进行处理,这些数据比较分散,如果一时网络抖动或者其他因素,数据就丢失了app --> kafka --> ...
- Callcenter 模块解析
CallCenter模块详细介绍 一. Callcenter模块说明: 提供了呼叫中心的ACD功能,把客户端通过不同的”方案”和”等级”分配给来电,一个以”评分”为基础的系统是用来分配这些呼入.来电者 ...
- Solidworks设计电路外形导入AltiumDesigner
将实际设计好的三维电路图的底板(单个零件模式下)轮廓另存为dwf或者dwg 这时候会出现一个选项框,需要进行一些设置 单位选择mm,这个按照自己的需求选择单位 单位映射选择为1mm,也就是1:1的比例 ...
- 【原创】编程基础之Ruby
ruby2.6.2 官方:https://www.ruby-lang.org/en/ 一 简介 A dynamic, open source programming language with a f ...
- [javascript]multipart/form-data上传格式表单自定义创建
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- VI编辑器、ipython、jupyter及进程知识总结
一.VI编辑器 1.三种模式 一般模式(normal mode 默认) 插入模式(insert mode) 末行模式(last line mode) 2.模式切换 一般模式-------------- ...
- oracle 报表带小计合计
selectcase when (grouping(glbm)=1) then '合计' else DECODE(glbm,null,'',glbm) end glbm,case when (grou ...