1. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation: 1 <---
/ \
2 3 <---
\ \
5 4 <---

解1 层序遍历,每一层最后一个节点肯定是最右边的节点。在层序遍历时,用cur表示当前层的节点数,next表示下一层节点数,每次从队列里面出来一个,cur减1,当cur==0时,表明当前出队的节点已经是最右边的,然后令cur=next, next = 0;每进队一个节点,next增加1

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int>ans;
if(root == NULL)return ans; queue<TreeNode*>q;
q.push(root);
int cur = 1, next = 0;
while(!q.empty()){
TreeNode* tmp = q.front();
q.pop();
cur--;
if(tmp->left){
q.push(tmp->left);
next++;
}
if(tmp->right){
q.push(tmp->right);
next++;
}
if(cur == 0){
ans.push_back(tmp->val);
cur = next;
next = 0;
}
}
return ans;
}
};

解2 dfs

class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int>ans;
if(root == NULL)return ans;
dfs(root, 0, ans);
return ans;
}
void dfs(TreeNode* node, int level, vector<int>& ans){
if(level == ans.size())ans.push_back(node->val);
if(node->right)dfs(node->right, level+1, ans);
if(node->left)dfs(node->left, level+1, ans);
}
};

【刷题-LeetCode】199 Binary Tree Right Side View的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  2. leetcode 199 :Binary Tree Right Side View

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

  3. [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 ...

  4. 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 ...

  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二叉树右侧视角

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

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

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

随机推荐

  1. pymysql-sqlalchemy-orm

    sqlalchemy示例代码 多对多外键 场景:一个作者映射多个书籍,一个书籍有多个作者 作者表: id name email sex 1 Alex alex@.. M 2 Rail rail@.. ...

  2. input type 使用

    type属性值 hidden: 隐藏. text:文本 search:搜索 tel url email password:密码 date:日期选择器 month:月份选择器 week:周选择器 tim ...

  3. JAVAWEB统一返回格式Result类

    Result.java public class Result<T> { private Integer code; private String msg; private String ...

  4. Qt之QListView和QStandardItemModel用法

    note 个人理解, QListView 用于 显示数据,而数据的逻辑维护则由 QStandardItemModel 完成. QStandardItemModel 创建 if (nullptr == ...

  5. 1266 - Points in Rectangle

    1266 - Points in Rectangle    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  6. idea使用教程-模板的使用

    一.代码模板是什么 它的原理就是配置一些常用代码字母缩写,在输入简写时可以出现你预定义的固定模式的代码,使得开发效率大大提高,同时也可以增加个性化.最简单的例子就是在Java中输入sout会出现Sys ...

  7. 【Leetcode】718. 最长重复子数组

    最长重复子数组有一下性质 A: [1,2,3,2,1] B: [3,2,1,4,7]设横是A竖是B,有规律:若横元和竖元相等,则为1,不等为0 1 2 3 2 13 0 0 1 0 12 0 1 0 ...

  8. 【入门到精通】❤️「Java工程师全栈知识路线」

    持续更新中- Vue前端开发 章节 内容 实践练习 Vue.js高效前端开发 • (实践练习) 第1章 Vue.js高效前端开发 • [ 一.初识Vue.js ] 第2章 Vue.js高效前端开发 • ...

  9. 云南农职《JavaScript交互式网页设计》 综合机试试卷②——实现轮播图效果

    一.语言和环境 实现语言:HTML,CSS,JavaScript,JQuery. 开发环境:HBuilder. 二.题目(100分): 使用JQuery淡入淡出动画,实现轮播图效果 每隔2秒钟切换一张 ...

  10. 编写Java程序,使用 dom4j 创建一个 XML 文档,文档名为“city.xml”。注意该文档的格式和数据

    查看本章节 查看作业目录 需求说明: 使用 dom4j 创建一个 XML 文档,文档名为"city.xml".该文档的格式和数据如图所示 实现思路: 创建Java项目,添加dom4 ...