项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.问题描述

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.

For example:
Given the following binary tree,

  1            <---/   \
2     3         <---\     \5     4       <---

2.解题思路

这道题最容易理解的思路是使用层次遍历的方法去解。
层次遍历的时候,分层记录每一层的所有节点,然后输出该层最右边的节点即可。

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;/*** Created by wanglei on 19/4/14.*/
public class BinaryTreeRightSide {public static TreeNode<Integer> init() {TreeNode<Integer> root = new TreeNode<>(1);TreeNode<Integer> node2 = new TreeNode<>(2);TreeNode<Integer> node3 = new TreeNode<>(3);TreeNode<Integer> node4 = new TreeNode<>(4);TreeNode<Integer> node5 = new TreeNode<>(5);root.left = node2;root.right = node3;node3.right = node4;node2.right = node5;return root;}public static void rightSide1(TreeNode<Integer> root) {Deque<TreeNode<Integer>> queue = new LinkedList<>();List<List<Integer>> result = new ArrayList<>();if (root != null) {queue.offer(root);}while(queue.size() > 0) {int levelnum = queue.size();List<Integer> inner = new ArrayList<>();for(int i=0; i<levelnum; i++) {TreeNode<Integer> tmp = queue.poll();inner.add(tmp.data);if(tmp.left != null) {queue.offer(tmp.left);}if(tmp.right != null) {queue.offer(tmp.right);}}result.add(inner);}for(List<Integer> innerlist: result) {System.out.println(innerlist.get(innerlist.size() - 1));}}public static void main(String[] args) {TreeNode<Integer> root = init();rightSide1(root);}
}

输出结果为:

1
3
4

Binary Tree Right Side View 二叉树右视图相关推荐

  1. Leetcode199二叉树右视图[C++题解]:BFS+层数

    文章目录 题目 思路 提交代码(ac代码) 测试代码 题目 Leetcode199二叉树右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入 ...

  2. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  3. 199. Binary Tree Right Side View

    /** 199. Binary Tree Right Side View * 11.21 By Mingyang * 在recursive的算法,就是贴着树的右边界往下面走,如果不行往左边偏一个,然后 ...

  4. LeetCode199. Binary Tree Right Side View

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

  5. leetcode 199. Binary Tree Right Side View | 199. 二叉树的右视图(Java)

    题目 https://leetcode-cn.com/problems/binary-tree-right-side-view/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetco ...

  6. LeetCode Binary Tree Right Side View(搜索)

    问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...

  7. LeetCode Binary Tree Right Side View (DFS/BFS)

    题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. 1 /** 2 * Definition for a binary ...

  8. 记录层序遍历中每层右侧第一个数字 Binary Tree Right Side View

    为什么80%的码农都做不了架构师?>>>    问题: Given a binary tree, imagine yourself standing on the right sid ...

  9. 114. Flatten Binary Tree to Linked List 二叉树展开为链表

    给定一个二叉树,原地将它展开为一个单链表. 例如,给定二叉树 1/ \2 5/ \ \ 3 4 6 将其展开为: 1\2\3\4\5\6 前序遍历 将二叉树展开为单链表之后,单链表中的节点顺序即为二叉 ...

  10. 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和

    Title 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 ...

最新文章

  1. linux怎样创建硬链接,Linux下创建软、硬链接
  2. 【Python金融量化 7- 100 】、七、计算两只股票方差和相关性
  3. 【Java多线程】并发容器CopyOnWriteArrayList
  4. Android之自定义ContentProvider详解
  5. 古巴比伦乘法_古巴:为生产做准备
  6. 再度吐槽,PHP在centos7的安装方式稍不注意可能就打击你的积极性
  7. 诗与远方:无题(二十六)- 曾经给一个妹子写的一首诗
  8. 从零开始学Pytorch(三)之多层感知机的实现
  9. html语言 大全,HTML语言大全
  10. 令人期待的php7.4,PHP7.4新特性
  11. 《Android深度探索》(卷1)HAL与驱动开发 第十章心得体会
  12. android---gettag()与settag()的妙用
  13. html当当图书榜页面,2019书排行榜_当当网图书排行榜
  14. 摩尔吧 FPGA培训
  15. 突破限制轻松下载网盘的文件,免登陆使用,速度也不错!
  16. 做PPT不要傻乎乎直接插入图片,一键处理,秒变高逼格
  17. 《Learning Discriminative Features with Multiple Granularities for Person Re-Identification》论文阅读之MGN
  18. QT设置背景图片的Qss实现方式
  19. Romax在法雷奥研发低功耗电驱动系统中的应用
  20. c语言英文排版程序,C语言设计—英文排版系统精品.docx

热门文章

  1. POJ-3259-Wormholes
  2. 8-C++远征之继承篇-学习笔记
  3. 数据中心效率:40%的改进是通过最佳实践方案
  4. 视频云存储平台 备忘
  5. final变量属性小记
  6. 编程语言的通用概念[共同特征]
  7. SAP OLE中常用的一些方法和属性
  8. springboot定制404错误信息
  9. 四万个与RIG漏洞利用套件相关的子域名遭到关闭
  10. shell读取用户输入