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 ...
随机推荐
- 第一章:什么是Linux
Linux可以分为四部分: Linux 内核:系统内存管理,软件程序管理,硬件设备管理和文件系统管理 GNU 工具:除了内核控制硬件系统,操作系统还需要一些工具执行标准功能,比如控制文件和程序.包括核 ...
- POJ-2299 Ultra-QuickSort (树状数组)
题目链接:Ultra-QuickSort 题意: 给出了一个序列,序列中有n个数,现在每次操作能交换相邻的两个数,要求操作几次可以将这个序列转换为一个从小到大排序的序列. 题解: 我的解法是先把所有的 ...
- Java类的加载的一个小问题
前言 之前写了一篇文章专门介绍了一下类的加载和对象的创建流程,然后收到了一个博友的疑问,觉得蛮好的,在这里和大家分享下. 博文地址:[Java基础]Java类的加载和对象创建流程的分析 疑问 类在加载 ...
- AJAX其实就是一个异步网络请求
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).其实就是一个异步网络请求. 一.创建对象 var xmlhttp; if (w ...
- 一款好看的Sublime Text浅色主题:Ayu大作
上一篇分享的VS Code的文中,界面也是浅色主题,也是Ayu作品.下面看一下Sublime Text中的Ayu浅色主题 不错吧. 如何下载? 首选项——插件控制——安装插件或者ctrl+shift+ ...
- 用Angule Cli创建Angular项目
Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...
- Netty源码分析第1章(Netty启动流程)---->第2节: NioServerSocketChannel的创建
Netty源码分析第一章: Server启动流程 第二节:NioServerSocketChannel的创建 我们如果熟悉Nio, 则对channel的概念则不会陌生, channel在相当于一个通 ...
- Netty源码分析第5章(ByteBuf)---->第2节: ByteBuf的分类
Netty源码分析第五章: ByteBuf 第二节: ByteBuf的分类 上一小节简单介绍了AbstractByteBuf这个抽象类, 这一小节对其子类的分类做一个简单的介绍 ByteBuf根据不同 ...
- HPUX系统启动后主机名为unknown的解决办法
HPUX系统启动完成后,主机名为unknown,查看/etc/rc.log出现如下报错: unknown:[/]grep -i error /etc/rc.log /sbin/rc1.d/S320 ...
- JavaScript设计模式-----模板方法模式
模板方法模式是一种只需要使用继承就可以实现的非常简单点的模式. 模板方法模式有两部分组成,第一部分是抽象父类,第二部分是具体的实现子类.通常在抽象父类中封装了子类的算法框架,包括实现 一些公共方法以及 ...