[抄题]:

给定二叉树,返回其节点值的垂直遍历顺序。 (即逐列从上到下)。
如果两个节点在同一行和同一列中,则顺序应 从左到右

给定一个二叉树 {3,9,20,#,#,15,7}

   3
/\
/ \
9 20
/\
/ \
15 7

返回垂直遍历顺序:[[9],[3,15],[20],[7]]

给定一个二叉树 {3,9,20,#,#,15,7}

     3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7

返回垂直遍历顺序:[[4],[9],[3,0,1],[8],[7]]

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 完全不知道怎么找出数学关系,找出变化关系:列数从小到大,同一列时行数从小到大,从左到右
  2. 从左到右要用bfs来实现:添加一个点、扩展;添加一个点、扩展

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 不知道bfs怎么写,怎么扩展:树上进行的BFS是用左右节点来扩展的
  2. queue是用linkedlist实现的,不要死记硬背,直接联想图吧 用的是.poll()方法
  3. 已经创建出了qcol对象,判断它是否为空,用isempty(),没有对象才用null,要理解含义

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

树上进行的BFS是用左右节点来扩展的

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 每一列后面都是数字链表,用hashmap的value存储数字链表,和叶子顺序遍历相同
  2. 用两个队列:列数也是不断添加一个列、扩展到新涉及的列;添加一个列、扩展到新涉及的列
  3. hash.keySet()方法(方法也符合骆驼命名法)表示取出哈希表的所有key,Collections.min方法表示取出没有排序的哈希表的其最小值

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: the root of tree
* @return: the vertical order traversal
*/
public List<List<Integer>> verticalOrder(TreeNode root) {
//corner case
List<List<Integer>> ans = new ArrayList<>();
HashMap<Integer, List<Integer>> hash = new HashMap<>(); if (root == null) {
return ans;
}
Queue<Integer> qCol = new LinkedList<>();
Queue<TreeNode> qNode = new LinkedList<>();
//bfs
qCol.offer(0);
qNode.offer(root); while (!qCol.isEmpty()) {
int col = qCol.poll();
TreeNode node = qNode.poll(); hash.putIfAbsent(col,new ArrayList());
hash.get(col).add(node.val); if (node.left != null) {
qCol.offer(col - 1);
qNode.offer(root.left);
} if (node.right != null) {
qCol.offer(col + 1);
qNode.offer(root.right);
}
} for (int i = Collections.min(hash.keySet()); //
i <= Collections.max(hash.keySet()); i++) {
ans.add(hash.get(i));
} return ans;
}
}

二叉树垂直遍历 · Binary Tree Vertical Order Traversal的更多相关文章

  1. [Swift]LeetCode314. 二叉树的竖直遍历 $ Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  5. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  6. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  7. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  8. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的垂直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  9. [leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

随机推荐

  1. I.MX6 PWM buzzer driver hacking with Demo test

    /***************************************************************************** * I.MX6 PWM buzzer dr ...

  2. POJ2728 Desert King 最优比率生成树

    题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...

  3. HDU2888 Check Corners(二维RMQ)

    有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...

  4. p/Invoke工具

    开源的工具 下面这个链接来下载这个工具: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a06085 ...

  5. c++交叉#include问题

    这个问题会引起莫名其妙的编译错误, 碰到类里有其他类的指针的话,不要#include,提前声明下即可 class_a.h #ifndef CLASS_A_H #define CLASS_A_H cla ...

  6. php non-thread-safe和thread-safe这两个版本有何区别?

    php non-thread-safe和thread-safe这两个版本有何区别? non-thread-safe 非线程安全 与IIS 搭配环境thread-safe 线程安全 与apache 搭配 ...

  7. 解决hue查询中文报错问题

    hue 4.0查询查询中包含中文报一下错误 (1366, Incorrect string value: \\xE4\\xBA\\xAC\\xE4\\xB8\\x9C... for column se ...

  8. 转- 集群NAS技术架构

    集群NAS技术架构 标签: 集群存储负载均衡扩展服务器网络 原贴:http://blog.csdn.net/liuaigui/article/details/6422700 作者刘爱贵 1 什么是集群 ...

  9. virtualenv基本使用

    win 安装 virtualenv pip3 install virtualenv 创建虚拟环境 virtualenv env1 进入虚拟环境 env1/Scripts/activate 退出虚拟环境 ...

  10. bzoj2865 字符串识别

    Description XX在进行字符串研究的时候,遇到了一个十分棘手的问题. 在这个问题中,给定一个字符串S,与一个整数K,定义S的子串T=S(i, j)是关于第K位的识别子串,满足以下两个条件: ...