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, ...
随机推荐
- [PHP] 算法-找出两个链表的第一个公共结点的PHP实现
输入两个链表,找出它们的第一个公共结点 1.两个单链表,有公共结点,那么必然,尾部公用 2.找出链表1的长度,找出链表2的长度,长的链表减去短的链表得出一个n值 3.长的链表先走n步,两个链表再同时移 ...
- HibernateTemplate的用法以及作用
HibernateTemplate作用:从字面上意思我们就知道他是一个模板,然后我们又知道hibernate是一个对象关系映射的框架,所以我们很容易联想到他的功能就是将Hibernate 的持久层访问 ...
- vue组件+axios访问本地json
import axios from 'axios'export default { name: "AjaxText", data: function () { return { m ...
- cgi、fastcgi及php-fpm分别是什么
cgi cgi是通用网关接口定义.当web server收到/index.php这个请求后,会启动对应的CGI程序,这里就是PHP的解析器.接下来PHP解析器会解析php.ini文件,初始化执行环境, ...
- [HTML/CSS]有一种节点叫做文本节点
HTML可以看成是由节点(node)组成的树结构 我们一般都是在<p>节点里面写字符串. 在上图中,<p>节点和字符串之间有一个text, 这个text就是文本节点. 我们可以 ...
- jquery绑定点击事件的三种写法
一.用jquery动态绑定点击事件的写法 部分代码: <script type="text/javascript"> $(document).ready(functio ...
- 安卓开发_浅谈DatePicker(日期选择器)
DatePicker继承自FrameLayout类,日期选择控件的主要功能是向用户提供包含年.月.日的日期数据并允许用户对其修改.如果要捕获用户修改日期选择控件中的数据事件,需要为DatePicker ...
- echart参数设置——曲线图
{ title: { text: '请求返回码分布', subtext: '实时数据' }, tooltip: { trigger: 'axis', position: function (point ...
- 【锁】Oracle死锁(DeadLock)的分类及其模拟
[锁]Oracle死锁(DeadLock)的分类及其模拟 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不 ...
- 【转】解决weblogic启动慢和创建域慢的方法
创建域慢启动慢的特征:创建域到70%时,进程长时间等待(命令行创建时停止在100%处),创建域启动Weblogic的时候也是长时间停止. Weblogic在Linux下启动慢的原因,发现从进程堆来看, ...