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
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的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- (二叉树 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 ...
- [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 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【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, ...
随机推荐
- 菜鸟入门【ASP.NET Core】3:准备CentOS和Nginx环境转(转)
基本软件 VMware虚拟机 centos:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-170 ...
- Flask的Context(上下文)学习笔记
上下文是一种属性的有序序列,为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等. 比如在计算机中,相对于进程而言,上下文就是进 ...
- Django Rest framework 之 认证
django rest framework 官网 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest fra ...
- SVN SVN合并(Merge)与拉取分支(Branch/tag)操作简介
SVN合并(Merge)与拉取分支(Branch/tag)操作简介 合并(Merge) 例子:把对feature_branch\project_name_v3.3.7_branch的修改合并到deve ...
- 修改eclipse的背景色(转载)
eclipse操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些. 设置方法如下: 1.打开window-> ...
- 怎样制作爽心的 dashboard ?
在目前的大数据趋势中,数据的大屏可视化成为大家所推崇的一种互动展示模式.如果我们能够早一些了解和掌握这方面的技术,相信对我们的未来将会非常有帮助! 我们知道,通过报表工具实现大屏展示可以通过单张报表. ...
- LyX使用中的一些问题
编译开始产生的检查错误 试用LyX2.3,在2.15中能编译通过的文档,竟然提示错误 The user preamble of your document contains glyphs that a ...
- scala spark 机器学习初探
Transformer: 是一个抽象类包含特征转换器, 和最终的学习模型, 需要实现transformer方法 通常transformer为一个RDD增加若干列, 最终转化成另一个RDD, 1. 特征 ...
- 慕学在线网0.2_users表设计(2)
接着上一篇,我们继续对users表进行完善.其中包括邮箱验证码(EmailVerifyRecord)和轮播图(PageBanner)两个model. 1.编写"models.py" ...
- 安装mysql(macos系统)
1.到官网下载,直接百度"mysql"即可找到 2.双击安装包,一路安装即可 3.然后进到系统设置 4.接下来 输入coho的用户密码 设置root用户的密码 切换root用户,v ...