Print Nodes in Top View of Binary Tree
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n)
A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.
1
/ \
2 3
/ \ / \
4 5 6 7
Top view of the above binary tree is
4 2 1 3 7 1
/ \
2 3
\
4
\
5
\
6
Top view of the above binary tree is
2 1 3 6
We strongly recommend to minimize your browser and try this yourself first.
The idea is to do something similar to vertical Order Traversal. Like vertical Order Traversal, we need to nodes of same horizontal distance together. We do a level order traversal so that the topmost node at a horizontal node is visited before any other node of same horizontal distance below it. Hashing is used to check if a node at given horizontal distance is seen or not.
/Print a Binary Tree in Vertical Order
static int min;
static int max;
static int[] output; public class Item{
public Integer dis;
public TreeNode root;
public Item(Integer dis, TreeNode root){
this.root = root;
this.dis = dis;
}
}
static int min;
static int max;
static int[] output; public static void findMinMax(TreeNode root, Integer dis){
if(root == null) return;
else{
min = Math.min(dis, min);
max = Math.max(dis, max);
}
findMinMax(root.left, dis - 1);
findMinMax(root.right, dis + 1);
} public static void levelOrder(TreeNode root){
LinkedList<Item> queue = new LinkedList<Item>();
queue.add(new Item(0, root));
while(!queue.isEmpty()){
Item tmp = queue.poll();
// if(output[tmp.dis - min] == 0){
output[tmp.dis - min] = tmp.root.val;
// }
if(tmp.root.left != null) queue.add(new Item(tmp.dis - 1, tmp.root.left));
if(tmp.root.right != null) queue.add(new Item(tmp.dis + 1, tmp.root.right));
}
} public static int[] verticalOrderTraveralBT(TreeNode root){
min = 0; max = 0;
findMinMax(root, 0);
int len = max - min + 1;
output = new int[len];
levelOrder(root);
return output;
} public static void main(String[] args) {
// int[] p = new int[]{10, 20, 30, 40, 30};
// System.out.println(MatrixChainMultiplication(p)); TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
root.right.left.right = new TreeNode(8);
root.right.right.right = new TreeNode(9); /* Create following Binary Tree
1
/ \
2 3
\
4
\
5
\
6*/
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
// root.right = new TreeNode(3);
// root.left.right = new TreeNode(4);
// root.left.right.right = new TreeNode(5);
// root.left.right.right.right = new TreeNode(6);
int[] result = verticalOrderTraveralBT(root);
System.out.println(result);
}
如果是top view 就把 if(output[tmp.dis - min] == 0){ uncomment
Print Nodes in Top View of Binary Tree的更多相关文章
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- leetcode笔记(二)94. Binary Tree Inorder Traversal
题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Leetcode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- #2009. 「SCOI2015」小凸玩密室
神仙题啊.完全想不出 首先看方案.可以从任意一个点开始,在这个点要先走完子树,然后走到父亲,再走兄弟,再走父亲的父亲,父亲的兄弟..一直走到1,1的另外一个子树,结束. 完全不会鸭.jpg 设f[i] ...
- Distributed2:SQL Server 创建分布式数据库
分布式数据库的优势是将IO分散在不同的Physical Disk上,每次查询都由多台Server的CPU,I/O共同负载,通过各节点并行处理数据来提高性能,劣势是消耗大量的网络带宽资源,管理难度大.在 ...
- 【linux报错】安装好虚拟机后,挂载光盘报错:mount:you must specify the filesystem type
问题现象: 问题原因: 当时光盘的“已连接”的勾没有勾上 解决后:
- Zabbix实战-简易教程--业务类
一.需求 项目要求对线上服务器进行监控,包括服务器本身状态.进程相关数据.业务相关数据. 服务器本身状态可以通过基础模板即可获取数据(CPU.内存.网络.磁盘): 进程相关数据,前面也有相关文章专门监 ...
- [VB.NET][C#]WAV格式文件头部解析
简介 WAV 为微软开发的一种声音文件格式,它符合 RIFF(Resource Interchange File Format)文件规范,用于保存 Windows 平台的音频信息资源. 第一节 文件头 ...
- String字符串的方法
String字符串在Java开发中是我们常用的一种数据类型,同时String字符串也为我们提供了大量的方法.通过一些实例的练习,我们可以对String字符串的方法有一个比较清楚的了解. 有一个字符串S ...
- 安装VMware-tools时,一直停在“The path "" is not valid path to the gcc binary.”
解决方案: 1.先停止安装(ctrl+Z) 2.在终端输入: yum -y update yum -y install kernel-headers kernel-devel gcc 3.重新安装VM ...
- skipfish介绍
skipfish 开发语言:C语言 命令行扫描器 主动扫描web安全评估工具 谷歌开发 已经不再进行维护 重点关注web代码 通过两种方式进项扫描:1.字典枚举 2.递归爬网 优点:速度快.支持多路单 ...
- Hyperledger Fabric(v1.2.0)代码分析1——channel创建
Hyperledger Fabric(v1.2.0)代码分析1--channel创建 0. e2e_cli Hyperledger Fabric提供了一个e2e的例子,该例中创建了一个基础的区块链网络 ...
- Django_rest_framework_版本(待验证)
简介 API版本控制可以用来在不同的客户端使用不同的行为.REST框架提供了大量不同的版本设计. 版本控制是由传入的客户端请求决定的,并且可能基于请求URL,或者基于请求头. 有许多有效的方法达到版本 ...