leetcode 199 :Binary Tree Right Side View
// 我的代码
package Leetcode;
/**
* 199. Binary Tree Right Side View
* address: 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.
*
*/
import java.util.*; public class BinaryTreeRightSideView {
public static void main(String[] args) {
TreeNode[] tree = new TreeNode[9];
for (int i = 1; i < 9; i++){
tree[i] = new TreeNode(i);
}
TreeNode.link(tree, 1, 2, 3);
TreeNode.link(tree, 2, 4, 5);
TreeNode.link(tree, 3, 6, -1);
TreeNode.link(tree, 5, 7, 8);
// 先序遍历
TreeNode.prePrint(tree[1]);
// solution
List<Integer> result = rightSideView(tree[1]);
System.out.println(result); // solution2
List<Integer> result2 = rightSideView2(tree[1]);
System.out.println(result2); }
/**
* 方法一:时间复杂度较高,需要 O(n),n为树中节点的个数
* @param root
* @return
*/
public static List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
int childNum = 0;
if (root != null){
queue.offer(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left!= null){
queue.offer(node.left);
childNum++;
}
if (node.right != null)
{
queue.offer(node.right);
childNum++;
}
System.out.println("queue.size() = " + queue.size());
if (childNum - queue.size() == 0){
result.add(node.val);
childNum = 0;
}
}
} return result; }
/**
* 方法二
* 别人家的解法,巧妙,速度快且好理解
* @param root
* 中右左 深度优先遍历,保存每层的第一个节点。用当前结果表的size作为标识,这样保证每次存的节点都是第一次出现在该层的节点
* @return
*/
public static List<Integer> rightSideView2(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null){
return res;
}
dfs (root, res, 0);
return res;
} public static void dfs (TreeNode root, List<Integer> res, int level){
if (root == null){
return;
}
if (res.size() == level){
res.add (root.val);
}
if (root.right != null){
dfs (root.right, res, level + 1);
}
if (root.left != null){
dfs (root.left, res, level + 1);
}
}
leetcode 199 :Binary Tree Right Side View的更多相关文章
- 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 (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, ...
随机推荐
- RBAC权限模型
RBAC 现在大多数的管理系统都是基于RBAC开发的组织机构权限框架.所有的操作都是基于角色(Role)来完成的.我们先从需求的角度出发,来了解关于系统权限管理. 用户A和用户B都属于研发部,我们可以 ...
- Cosmos —— Big Data at Microsoft
1, 1,cosmos stores. Cosmos stores data as streams – a file-like structure Streams are split apart in ...
- 各种Android手机Root方法
Root的介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限.我们root手机的过程 ...
- 基于SS5服务端的Socks5客户端
SS5停止更新已经好几年了,用作socks5代理的服务端还是比较稳定的.但是如果要使用加密账号和密码的协议,有些坑需要去填. 1.服务端的账号密码验证方式配置为“s”时,客户端进行协议验证时,需要用“ ...
- -bash: fork: retry: Resource temporarily unavailable
登陆不了服务器The server refused to start a shell. 登陆服务器后执行ls命令报错: 1 2 $ls -bash: fork: retry: Resource t ...
- Debian-based Linux distributions 安装 virtualbox
Add the following line to your /etc/apt/sources.list: deb http://download.virtualbox.org/virtualbox/ ...
- jdbcTemplate批量插入(添加)
public void addSubscibe(List<PermedipUserSubscribeVo> list) { final List<PermedipUserSubscr ...
- SQL Server 从数据库中查询去年的今天的数据的sql语句
因为最近的项目的一个小功能需要实现当前数据和历史的今天做一个对比.在网上也查了很久,很多都是实现一个月内的,一年内的所有数据,昨晚突然就找到了下面的实现方法,在SQL Server2008中试了一下, ...
- MysqlNDB集群配置
为了避免不必要的资源分配,默认情况下是不启动ndbcluster引擎.
- splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目
删除位于 index 2 的元素,并添加一个新元素来替代被删除的元素: <script type="text/javascript"> var arr = new Ar ...