[LC] 314. Binary Tree Vertical Order Traversal

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 1:

Input: [3,9,20,null,null,15,7]

   3
  / /   9  20
    /   /    15   7 

Output:

[
  [9],
  [3,15],
  [20],
  [7]
]
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    public List<List<Integer>> verticalOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        dfs(root, 0);
        for (int i = min; i <= max; i++) {
            res.add(new ArrayList<>());
        }

        Queue<TreeNode> queue = new LinkedList<>();
        Queue<Integer> indexQ = new LinkedList<>();
        queue.offer(root);
        // map the leftmost to 0, so that root is -min
        indexQ.offer(-min);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            int curIndex = indexQ.poll();
            res.get(curIndex).add(cur.val);
            if (cur.left != null) {
                queue.offer(cur.left);
                indexQ.offer(curIndex - 1);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
                indexQ.offer(curIndex + 1);
            }
        }
        return res;
    }

    private void dfs(TreeNode root, int num) {
        if (root == null) {
            return;
        }
        min = Math.min(min, num);
        max = Math.max(max, num);
        dfs(root.left, num - 1);
        dfs(root.right, num + 1);
    }

}

原文地址:https://www.cnblogs.com/xuanlu/p/12210686.html

时间: 2024-08-05 15:10:33

[LC] 314. Binary Tree Vertical Order Traversal的相关文章

314. Binary Tree Vertical Order Traversal

题目: 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,nul

Leetcode 314. Binary Tree Vertical Order Traversal

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,n

[Locked] Binary Tree Vertical Order Traversal

Binary Tree Vertical Order Traversal 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. Examp

LeetCode Binary Tree Vertical Order Traversal

原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: 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, th

[LintCode] Binary Tree Vertical Order Traversal

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. Have you met this question in a real inter

*Binary Tree Vertical Order Traversal

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,nu

Binary Tree Vertical Order Traversal -- LeetCode

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,n

[LC] 102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

[LC] 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level order