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

You should return [1, 3, 4].

解题思路:

DFS,带个height即可,JAVA实现如下:

	public List<Integer> rightSideView(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
list.add(root.val);
if (root.right != null)
dfs(list, root.right, 1);
if (root.left != null)
dfs(list, root.left, 1);
return list;
} static void dfs(List<Integer> list, TreeNode root, int height) {
if (height == list.size())
list.add(root.val);
if (root.right != null)
dfs(list, root.right, height+1);
if (root.left != null)
dfs(list, root.left, height+1); }

Java for LeetCode 199 Binary Tree Right Side View的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

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

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

  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. (二叉树 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 ...

  6. [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. Java for LeetCode 107 Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

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

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

随机推荐

  1. 旅行(Dijkstra)问题

    问题:输入: 输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个:   接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车 ...

  2. codevs1064 虫食算

    题目描述 Description 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045    +    8468#6 ...

  3. vim YouCompleteMe

    http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html

  4. POJ2676Sudoku(类似于八皇后)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16444   Accepted: 8035   Special ...

  5. threading使用心得

    需求:监控多个重要网站(多线程),出现访问异常重试2次,第三次开始告警. 日志模块 import logging import logging.config # 日志配置 LOGCONF_FILENA ...

  6. ExtJS入门教程01,Window如此简单,你怎能不会?

    这是一系列ExtJS教程,今天的是第一篇,主要介绍ExtJS中Window的基本用法.希望大家能够支持! 来吧,创建一个漂亮的弹出窗 var win = Ext.create("Ext.Wi ...

  7. MySQL的表分区详解

    这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...

  8. MyEclipse------快速写入内容到指定目录下的文件(字节输出流)

    other.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  9. 转载大神的检测网站重定向的python脚本

    #!/usr/bin/env python #coding=utf8 import sys import requests def check_for_redirects(url): try: r = ...

  10. jquery uploadify 进入页面请求两次问题解决办法。

    this.settings.upload_url = SWFUpload.completeURL(this.settings.upload_url);this.settings.button_imag ...