leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/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.
For example:
Given the following binary tree,
- 1 <---
- / \
- 2 3 <---
- \ \
- 5 4 <---
You should return [1, 3, 4]
.
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class node {
- public:
- TreeNode *nd;
- int lv;
- node(TreeNode *rhs, int l): nd(rhs), lv(l) {}
- };
- class Solution {
- public:
- vector<int> rightSideView(TreeNode* root) {
- vector<pair<int, int> > load;
- vector<int> res;
- if(root == NULL) return res;
- stack<node> q;
- q.push(node(root, ));
- int lv = ;
- while(!q.empty()) {
- node top = q.top();
- int cur_lv = top.lv;
- q.pop();
- load.push_back(make_pair(top.nd->val, cur_lv));
- if(top.nd->left) q.push(node(top.nd->left, cur_lv+));
- if(top.nd->right) q.push(node(top.nd->right, cur_lv+));
- }
- int elv = ;
- for(int i=; i<load.size(); ++i) {
- if(elv == load[i].second) {
- res.push_back(load[i].first);
- ++elv;
- }
- }
- return res;
- }
- };
leetcode@ [199] Binary Tree Right Side View (DFS/BFS)的更多相关文章
- 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 、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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
- 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 ...
- [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 102 Binary Tree Level Order Traversal(DFS||BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
随机推荐
- Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 在非MFC程序中引用CString
CString在当今软件设计界里还是小有名气的,说它是MFC中使用的最多的类一点也不过,然而在使用sdk编windows程序的时候,确不能利用CString类,只能用sdk的运行时库,比如strlen ...
- POJ 2275 Flipping Pancake
点我看题目 题意 : 按我自己的理解就是,给你n个数,按照从上到下排列,现在让你进行一系列的操作,让你将数按照从大到小排好. 思路 : 比赛的时候以为要用记录路径的搜索,当时没什么把握,所以没做,今天 ...
- Servlet课程0425(五) sendRedirect实现不同页面共享数据
Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...
- 如何引用传递String Boolean 等,并改变他们的值
如何引用传递String Boolean 等,并改变他们的值 采用list, 在存入位置改变list的值 如 list.add(true); list.remove(0); list.add(fals ...
- Android 解析XML
public void getXML(String url) throws XmlPullParserException,IOException,URISyntaxException { String ...
- Ubuntu 12.04下搭建Qt开发环境
http://download.qt.io/official_releases/qt/ Ubuntu 环境下Gtk与Qt编译环境安装与配置(系统环境是Ubuntu 12.04) 1.配置基础开发环境G ...
- 什么是WebService
举个例子:现在有5个项目,项目彼此独立,甚至都不是同一类语言进行开发的.这5个项目是:百度知道,百度贴吧,百度新闻,百度视频,百度百科.突然有一天,老板说:把这几个系统揉称一个大项目,起名直接叫做百度 ...
- Tomcat配置HTTPS方式
简要记录主要步骤备忘 1.进入到jdk下的bin目录 2.输入如下指令 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat ...
- ehcache 分布式集群同步数据实例
本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可.直奔主题,可运行的demo实例! 创建一个maven项目,配置pom pom.xml & ...