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的更多相关文章

  1. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  2. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  3. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  4. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  9. Leetcode Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. springboot之jar运行脚本

    一.现在的工程都将就独立和简单了,我们在使用springboot做开发或者其他框架做开发时,在linux上面执行的时候.总会写一下脚本,目的当然是为了更加好的运行程序.不然每次都手动输入一下命令,来调 ...

  2. jsp jstl 值空格拼接导致的异常

    红线标记处多了个空格,所以出现值错误

  3. $.ready和onload 区别

    1.jq ready()的方法就是Dom Ready 他的作用或者意义就是:在DOM加载完成后就可以可以对DOM进行操作. 一般情况先一个页面响应加载的顺序是,域名解析-加载html-加载js和css ...

  4. 快速上手IOT视觉化开发工具Node-RED

    作者:何信昱 现在有越来越多非专业背景但有创造热诚的Maker,想要进入物联网领域大展身手,但常常苦于无法撰写艰深难懂的程序代码,以及想要连接各种硬件与时下最流行的社群软件,却不知道如何使用开放给开发 ...

  5. windows下安装Mongodb_4.0.6最新版及常用命令

    今天下载了最新版Mongodb进行安装,发现相比较于以前,方便了很多,直接下载: 一.下载地址:https://www.mongodb.com/download-center/enterprise 二 ...

  6. python中@property和property函数使用

    1.基本的@property使用,可以把函数当做属性用 class Person(object): @property def get_name(self): print('我叫xxx') def m ...

  7. 【RabbitMQ】三种Exchange模式——订阅、路由、通配符模式

    https://blog.csdn.net/ww130929/article/details/72842234

  8. 《spark机器学习 第二版》 蔡立宇 分享 pdf下载

    链接:https://pan.baidu.com/s/15Y14eAnfj8zf5mXdixbVeQ 提取码:rkdt

  9. Laya中的Image、Texture、WebGLImage

    Image Image是Laya的一个UI组件,继承自Component. Image.bitmap属性,是AutoBitmap类型:AutoBitmap继承自Graphics,负责处理图片九宫格逻辑 ...

  10. HTML文件转Word文件格式

    这是我需要转换的HTML文件 第一步~ 使用我们的福昕阅读器将我们.html文件打开,如下图 第二步: 点击“文件”——“另存为”——选择一个你自己喜欢的位置存放文件,此时的文件已经被转换成了.pdf ...