leetcode 199. Binary Tree Right Side View

这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历。

依旧利用之前层次遍历的代码,每次大的循环存储的是一行的节点,最后一个节点就是想要的那个节点

class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
if(root == NULL)
return result;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
result.push_back(q.back()->val);
for(int i = q.size();i > ;i--){
TreeNode* node = q.front();
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
q.pop();
}
}
return result;
}
};

leetcode 116. Populating Next Right Pointers in Each Node

这个题和199题类似,也是层序遍历,且最后一个的node的next为NULL;

class Solution {
public:
Node* connect(Node* root) {
if(root == NULL)
return NULL;
queue<Node*> q;
q.push(root);
while(!q.empty()){
for(int i = q.size();i > ;i--){
Node* node = q.front();
q.pop();
if(i != )
node->next = q.front();
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
}
}
return root;
}
};

117. Populating Next Right Pointers in Each Node II

这个题与116不同在于,树不再是完全二叉树。对于递归的方法不同,对于非递归,同一个代码完全可以解决这两个问题

leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. leetcode@ [199] Binary Tree Right Side View (DFS/BFS)

    https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...

  4. [leetcode]199. Binary Tree Right Side View二叉树右侧视角

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  5. Java for LeetCode 199 Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  6. (二叉树 bfs) leetcode 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. [leetcode]199. Binary Tree Right Side View二叉树右视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  9. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

随机推荐

  1. javascript: checked 不可全选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. vue.js 键盘enter事件的使用

    在监听键盘事件时,我们经常需要检查常见的键值.Vue 允许为 v-on 在监听键盘事件时添加按键修饰符: <!-- 只有在 `keyCode` 是 13 时调用 `vm.submit()` -- ...

  3. java Name [jdbc/myjavadb] is not bound in this Context. Unable to find [jdbc].

    一.出错时的情况: 首先,这是一个servlet项目 1.项目的web.xml配置了:(后来发现不配置这个也行,但是tomcat一定要配置) <resource-ref> <desc ...

  4. CoreCRM 开发实录 —— 基于 AntDesign 的新 UI

    上一篇说到,因为有新朋友加入,对前端开发有了新的要求.原来基于 Bootstrap 的 UI 就不要了.在网上(其实是 GitHub 上)逛了几圈,最后使用了 antd-admin 这个框架做为基础模 ...

  5. 张钹院士:场景是当前AI产业化最大问题

    张钹院士:场景是当前AI产业化最大问题 https://mp.weixin.qq.com/s/TLdoi9cnY-Crr0FVp2ah6g 在世界机器人大会“青年创新创业专题论坛”上,清华大学人工智能 ...

  6. uCrop图片裁剪

    uCrop使用 github地址 https://github.com/Yalantis/uCrop然后clone或下载到本地,运行之. 效果预览 app/build.gradle compile ' ...

  7. (后端)excel设置日期格式的步骤

    在excel中设置日期格式,分直接设置和代码设置. 一.直接设置: 选取日期所在的单元格,单元格右键菜单中--设置单元格格式.在单元格格式窗口中选数字类型为“日期”在右边的列表框中选取相应的日期格式即 ...

  8. 常用的Git命令整理

    之前一直忙于项目苦于没有时间总结,今天刚好有时间特来总结一下在工作中常用到的代码版本管理器Git.至于为什么要用Git?Git相比SVN有哪些好处?我就不多说了,前人已经总结的很好.今天主要介绍的是常 ...

  9. 为什么 APM 能提升 IT 团队工作质量?

    “有必要吗?”这是很多 IT 专业人员在尝试向团队内部推荐应用程序性能管理价值时所面临的问题.APM(应用程序性能管理)能为公司节约成本,提高内部工作效率,并真实了解用户对公司的系统和产品是否满意.除 ...

  10. JMeter—前置处理器(九)

    参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第四节前置处理器前置处理器用来处理请求前的一些准备工作,比如参数设置.环境变变量设置等 一.BeanShell ...