http://www.geeksforgeeks.org/print-binary-tree-vertical-order/

 package algorithms;

 import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap; public class PrintVertical { private static TreeMap<Integer, List<TreeNode>> hm = new TreeMap<>(); public static void main(String[] args) {
TreeNode root = new TreeNode(1);
TreeNode two = new TreeNode(2);
TreeNode three = new TreeNode(3);
TreeNode four = new TreeNode(4);
TreeNode five = new TreeNode(5);
TreeNode six = new TreeNode(6);
TreeNode seven = new TreeNode(7);
TreeNode eight = new TreeNode(8);
TreeNode nine = new TreeNode(9);
root.left = two;
root.right = three;
two.left = four;
two.right = five;
three.left = six;
three.right = seven;
six.right = eight;
seven.right = nine;
printVertical(root);
for(Map.Entry<Integer, List<TreeNode>> entry: hm.entrySet()) {
System.out.println("key====="+entry.getKey());
List<TreeNode> temp = entry.getValue();
for(TreeNode node: temp){
System.out.println("node val======"+node.val);
}
}
} public static void printVertical(TreeNode root) {
helper(0, root);
} public static void helper(int cur, TreeNode root) {
if(root == null)
return; ArrayList<TreeNode> array;
if(hm.containsKey(cur)) {
array = (ArrayList<TreeNode>) hm.get(cur);
} else {
array = new ArrayList<TreeNode>();
}
array.add(root);
hm.put(cur, array);
helper(cur - 1, root.left);
helper(cur + 1, root.right);
}
}

<注> 需要注意的是使用TreeMap而不是HashMap来存遍历以后的数据。Better to use TreeMap in this case, as it will store the result in ascending order,

    which we are interested in.

Print a Binary Tree in Vertical Order的更多相关文章

  1. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  2. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

  4. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  5. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  6. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  7. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  8. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  9. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

随机推荐

  1. python的反转(切片)

    看下面代码吧,简单来说不如直接看代码.如下: #coding=utf-8 __author__ = 'debude' a = 'python' print a[::-1] #从最后n开始,每走一位都打 ...

  2. 关于angularjs指令

    一个指令用来引入新的HTML语法.指令是DOM元素上的标记,使元素拥有特定的行为.举例来说,静态的HTML不知道如何来创建和展现一个日期选择器控件.让HTML能识别这个语法,我们需要使用指令.指令通过 ...

  3. VS2015编译boost1.62

    VS2015编译boost1.62 Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有 ...

  4. 【HEOI2012】采花 BZOJ2743

    Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一 ...

  5. arm汇编指令

    ARM处理器的指令集可以分为跳转指令.数据处理指令.程序状态寄存器(PSR)处理指令.加载/存储指令.协处理器指令和异常产生指令6大指令 一.跳转指令 跳转指令用于实现程序流程的跳转 跳转指令分类 Ⅰ ...

  6. Python3.5在Windows 7下连接ORACLE数据库

    1.首先需要安装好oracle数据库,本机适用plsql连接数据库正常,记录下数据库名称 2.安装cx_oracle模块 pip install cx_Oracle 3.python中引入模块 imp ...

  7. MAC OS PHP

    Apache与PHP的配置 OSX自带了apache和php,但默认情况下没有开启,打开终端 sudo apachectl start 这时在浏览器中输入localhost应该就会出现apache标准 ...

  8. ng-repeat

    <html ng-app="compile"> <head> <script src="http://apps.bdimg.com/libs ...

  9. css初始化代码

    最近老有新项目开发,一直在找存留的CSS初始化代码,索性放到这里备份下, @charset "utf-8"; /* -------------------------------- ...

  10. CMS系统存储路径

    CMS系统特点:前后端分离 index.html 首页文件index.php 管理后台的页面 api文件夹: 提供的接口 caches文件夹: 缓存文件 html文件夹:  生成的静态页面 phpcm ...