今天的华师

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

3
/
9 20
/
15 7

return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]

思路:

树的层次遍历,技巧是用NULL作为每一行的结尾
问题是什么时候才能是结尾呢?
当遇到队列中front是NULL时,表明上一行已经处理完了,则上一行的子节点也处理完了(处理每个节点的时候,让他的孩子节点入队列),此时把NULL加入队列
当队列中只有一个NULL的时候,则遍历完毕

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 * 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>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
if (root == NULL)
return result;
vector<int> line;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while (Q.size()>1)
{
TreeNode *p = Q.front();
Q.pop();
if (p == NULL)
{
大专栏  leetcode简单题6> result.push_back(line);
line.clear();
Q.push(NULL);
}
else
{
line.push_back(p->val);
if (p->left != NULL)
{
Q.push(p->left);
}
if (p->right != NULL)
{
Q.push(p->right);
}
}
}
result.push_back(line);
reverse(result.begin(), result.end());
return result;
}
};

Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
reverse(digits.begin(),digits.end());
int len=digits.size();
int carry=1;
for(int i=0;i<len;i++)
{
int temp=digits[i]+carry;
if(temp==10)
{
digits[i]=0;
carry=1;
}
else
{
digits[i]=temp;
carry=0;
}
}
if(carry==1)
digits.push_back(1);
reverse(digits.begin(),digits.end());
return digits;
}
};

leetcode简单题6的更多相关文章

  1. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  2. Go: LeetCode简单题,简单做(sort.Search)

    前言 正值端午佳节,LeetCode也很懂.这两天都是简单题,早点做完去包粽子. 故事从一道简单题说起 第一个错误的版本 简单题 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最 ...

  3. LeetCode简单题(三)

    题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股 ...

  4. LeetCode简单题(二)

    题目一: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...

  5. LeetCode简单题(一)

    题目一: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...

  6. LeetCode简单题汇总

      1.两个数之和 给出一个整数数组,请在数组中找出两个加起来等于目标值的数, 你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index ...

  7. Leetcode简单题

    # Title Solution Acceptance Difficulty Frequency     1 Two Sum       44.5% Easy     2 Add Two Number ...

  8. LeetCode简单题(四)

    题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...

  9. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

随机推荐

  1. 吴裕雄--天生自然 PHP开发学习:MySQL 插入数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  2. 01 语言基础+高级:1-4 接口与多态_day09【继承、super、this、抽象类】

    day09[继承.super.this.抽象类] 三大特性——继承方法重写super关键字this关键字抽象类 教学目标能够解释类名作为参数和返回值类型能够写出类的继承格式能够说出继承的特点能够说出子 ...

  3. rabbitmq文档

    https://blog.csdn.net/hellozpc/article/details/81436980

  4. 13.docker 网络 docker NameSpace (networkNamespace)

    一. 案例 1.创建一个 container docker run -d --name test1 busybox /bin/sh -c "while true; do sleep 3600 ...

  5. DRF一对多序列化和反序列化

    models.py # 商品分类 class Category(models.Model): name = models.CharField(max_length=32) # 商品 class Goo ...

  6. 解决 Win7 远程桌面 已停止工作的问题

    Windows 7远程桌面登录时崩溃, 错误提示如下: 问题签名: 问题事件名称: APPCRASH 应用程序名: mstsc.exe 应用程序版本: 6.1.7601.18540 应用程序时间戳: ...

  7. [学习笔记]连通分量与Tarjan算法

    目录 强连通分量 求割点 求桥 点双连通分量 模板题 Go around the Labyrinth 所以Tarjan到底怎么读 强连通分量 基本概念 强连通 如果两个顶点可以相互通达,则称两个顶点强 ...

  8. Solving ordinary differential equations I(Nonstiff Problems),Exercise 1.2:A wrong solution

    (Newton 1671, “Problema II, Solutio particulare”). Solve the total differential equation $$3x^2-2ax+ ...

  9. iOS补位动画、沙漏效果、移动UITableViewCell、模拟贪吃蛇、拖拽进度等源码

    iOS精选源码 JHAlertView - 一款黑白配色的HUD之沙漏效果 继承UIButton的自定义按钮SPButton 用递归算法实现iOS补位动画 iOS 长按移动UITableViewCel ...

  10. 吴裕雄--天生自然python学习笔记:python 用 Open CV抓取脸部图形及保存

    将面部的范围识别出来后,可以对识别出来的部分进行抓取.抓取一张图片中 的部分图形是通过 pillow 包中的 crop 方法来实现的 我们首先学习用 pillow 包来读取图片文件,语法为: 例如,打 ...