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, ...
随机推荐
- .net导出excle无需任何插件,直接通过一个tablehtml实现
项目背景: 项目需要导出样式复杂的excl表格,主要是一些样式布局比较复杂 技术分析: 目前比较通用的实现方式有 1.借助微软的excle插件 2.通过NPOI插件实现 3.直接导出一个html(ta ...
- [PHP] 数据结构-反转链表PHP实现
1.常见方法分为迭代和递归,迭代是从头到尾,递归是从尾到头2.设置两个指针,old和new,每一项添加在new的后面,新链表头指针指向新的链表头3.old->next不能直接指向new,而是应该 ...
- 快速排序 java详解
1.快速排序简介: 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此 ...
- 手把手教你实现Confluence6.7.1安装与破解
Confluence是一个专业的企业知识管理与协同软件,也可以用于构建企业wiki. 一.准备工作 下载confluence6.7.1 wget https://downloads.atlassian ...
- vue项目sql图片动态路径引用问题
最近遇到一个vue动态图片路径的引用问题?明明路径是正确的但是却渲染不出图片!先看我慢慢说来!! 1.当我们把图片的路径放置在data(){return:{}}中的数组中的时候,然后通过v-for循环 ...
- Python 练习:三级菜单选择城市
info = { 'GuangDong':{ 'GuangZhou': ['TianHe', 'HaiZhu'], 'MaoMing': ['MaoNan', 'DianBai']}, 'ShanDo ...
- 【读书笔记】iOS-iOS敏捷开发
敏捷开发分为几个不同的门派,如:Scrum,XBreed,极限编程(XP Extreme Programming)和水晶方法等. 参考资料:<iOS传感器应用开发最佳实践>
- 【读书笔记】iOS-Apple的移动设备硬件
本书中有一个关键观点是:“硬件并不是特别重要,用户体验才是真正的杀手级应用.“尽管如此,多了解一些你使用的硬件的相关知识,对于整个项目来说是必备的,而对于设计和开发高质量的作品来说敢是不可或缺的. 人 ...
- Sublime Text3 安装 markdownediting插件 报错 Error loading syntax file "Packages/Markdown/Markdown.tmLanguage":
问题: Error loading syntax file "Packages/Markdown/Markdown.sublime-syntax": 解决方法: ./Data/Lo ...
- spring资源访问接口和资源加载接口
spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...