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. 【TJOI2015】线性代数

    题面 题解 要求的是 \[ \sum_{i=1}^n\sum_{j=1}^na_ia_jb_{i,j} - \sum_{i=1}^na_ic_i \] 可以看出这是一个最大权闭合子图问题 代码 #in ...

  2. com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\salary-card\target\salary-card-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\keystore\login_id_rsa 资源未找到

    com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\sala ...

  3. 由 Session 和 Cookie 的区别说起

    Session 和 Cookie 有什么区别? 最近面试被问到这个问题,和面试官一番讨论了解到面试官心里的答案后,我不太满意. 面对上面的问题,如果是刚毕业时的我,一定会毫不犹豫说出 Cookie 是 ...

  4. 解决在控制层springmvc框架发出的400状态的错误

    错误场景: 错误分析: 这也是我第一次遇到这个类型的异常,400响应状态代表:客户端发出的请求中携带的参数与服务器端接受的参数类型不匹配,进一步就是说我后台的实体类中数据类型为Date,而前台传递过来 ...

  5. 微信小程序充值及充值回调后的处理

    微信小程序的充值流程与 H5 或 公众号大致差不多,这里简单说一下前端在充值时候的一些操作流程. 用户在小程序中发起充值请求时,一般会先请求自己的服务器,将充值的参数发送给后端,然后后端会去请求微信充 ...

  6. [原]Asp.net Core 2.1.2 测试成功Ajax上传文件新解法

    利用layui框架可以上传文件调试拦截成功! [HttpPost] public IActionResult Method1(IFormFile file) { return Json(new{suc ...

  7. Python学习过程笔记整理(四)

    变量作用域 -分类 -全局(global):在函数外部定义:整个全局范围都有效 -局部(local):在函数内部定义:仅在局部范围有效 -提升局部变量为全局变量 -使用global -globals, ...

  8. 经典笔试题:用C写一个函数测试当前机器大小端模式

    “用C语言写一个函数测试当前机器的大小端模式”是一个经典的笔试题,如下使用两种方式进行解答: 1. 用union来测试机器的大小端 #include <stdio.h> union tes ...

  9. cpp-variable-lifetime

    #include <cstdio> #include <iostream> using namespace std; class TmpClass; void FuncScop ...

  10. 《python 经典实例》 分享 pdf下载

    链接:https://pan.baidu.com/s/1FzSsBfynqx5ll_OpcZGDHg提取码:ykgk