Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Binary Tree Vertical Order Traversal

Posted on February 15, 2018July 26, 2020 by braindenny

Binary Tree Vertical Order Traversal



Similar Problems:

  • Find Leaves of Binary Tree
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #binarytree, #inspiring

Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column).

If two nodes are in the same row and column, the order should be from left to right.

Examples:

  • Given binary tree [3,9,20,null,null,15,7],
  3
 /\
/  \
9  20
   /\
  /  \
 15   7

return its vertical order traversal as:

[
  [9],
  [3,15],
  [20],
  [7]
]
  • Given binary tree [3,9,8,4,0,1,7],
     3
    /\
  /   \
  9     8
 /\    /\
/  \  /  \
4  0  1   7

return its vertical order traversal as:

[
  [4],
  [9],
  [3,0,1],
  [8],
  [7]
]
  • Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0’s right child is 2 and 1’s left child is 5),
     3
   /  \
  /    \
  9     8
 /\    /\
/  \  /  \
4  0  1   7
    \/
   / \
  5   2

return its vertical order traversal as:

[
  [4],
  [9,5],
  [3,0,1],
  [8,2],
  [7]
]

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/binary-tree-vertical-order-traversal
## Basic Ideas: BFS + hashmap
##
## Complexity: Time O(n), Space O(n)
##
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import collections
class Solution:
    def verticalOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if root is None: return []
        d = collections.defaultdict(lambda: [])
        queue = collections.deque([(root, 0)])
        d[0].append(root.val)
        while len(queue) != 0:
            for k in range(len(queue)):
                (node, position) = queue.popleft()
                if node.left:
                    d[position-1].append(node.left.val)
                    queue.append((node.left, position-1))
                if node.right:
                    d[position+1].append(node.right.val)
                    queue.append((node.right, position+1))
        res = []
        for position in sorted(d.keys()):
            res.append(d[position])
        return res
linkedin
github
slack

Post Views: 9
Posted in MediumTagged #binarytree, #inspiring, redo

Post navigation

LeetCode: Missing Ranges
LeetCode: Range Addition

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.