今天的华师

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. thinkCMF图片上传选择已上传图片

    1.找到上传图片的模板页面 webuploader.html 在上传文件标签后面 添加 <li class=""><a href="#explorer& ...

  2. 2018.11.2JavaScript随笔

    构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型

  3. PAT Advanced 1069 The Black Hole of Numbers (20) [数学问题-简单数学]

    题目 For any 4-digit integer except the ones with all the digits being the same, if we sort the digits ...

  4. hasura graphql-engine v1.2.0 beta 版本

    hasura graphql-engine v1.2.0 提供了一个很不错的功能action,这个也是目前其他graphql 没有hasura 强大的 地方,使用action 我们可以更好的扩展has ...

  5. 03-string字符串和while循环

    目录 03-string字符串和while循环 1. string介绍 2. 字符串的运算 3. 下标及分片 4. 格式化输出 5. f-string格式化输出用法 6. 字符串方法 7. 布尔值,空 ...

  6. Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几

    首先是模型参数和网络结构的保存 #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...

  7. MySql 相关面试题

    1.mysql 慢查询 目的:通过慢查询日志,记录超过指定时间的 SQL 语句,优化 sql 查询 步骤:查看慢查询开启状态-->设置慢查询 http://www.cnblogs.com/luy ...

  8. ASP.NET core MVC动作过滤器执行顺序

    using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Threading.Tasks; namespace dotnet ...

  9. 11)PHP,单选框和复选框的post提交方式处理

    就是一个表单中会有input的checkbox形式,那么怎么处理,就有了问题,一般采用二维数组来处理 代码展示: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  10. xib下如何修改frame

    1.取消xib下Use Auto Layout 2.xcode->product->clean