Giter VIP home page Giter VIP logo

vertical-order-traversal-of-a-binary-tree's Introduction

Vertical Order Traversal of a binary tree

Given a binary tree, return the vertical order traversal of its nodes values.

For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).

Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).

Important ๐Ÿ˜‰: If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.

Binary Tree Vertical Order Traversal

Implementation :

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    class Node {
        TreeNode treeNode;
        int horizontalDistance, verticalDistance;
        Node(TreeNode treeNode, int horizontalDistance, int verticalDistance) {
            this.treeNode = treeNode;
            this.horizontalDistance = horizontalDistance;
            this.verticalDistance = verticalDistance;
        }
    }
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null)
            return res;
        Map<Integer, List<Node>> map = new HashMap<>();
        Queue<Node> q = new ArrayDeque<>();
        q.add(new Node(root, 0, 0));
        int minHd = Integer.MAX_VALUE, maxHd = Integer.MIN_VALUE;
        while(!q.isEmpty()) {
            Node node = q.poll();
            map.putIfAbsent(node.horizontalDistance, new ArrayList<Node>());
            map.get(node.horizontalDistance).add(node);
            minHd = Math.min(minHd, node.horizontalDistance);
            maxHd = Math.max(maxHd, node.horizontalDistance);
            if(node.treeNode.left != null)
                q.add(new Node(node.treeNode.left, node.horizontalDistance - 1, node.verticalDistance - 1));
            if(node.treeNode.right != null)
                q.add(new Node(node.treeNode.right, node.horizontalDistance + 1, node.verticalDistance - 1));
        }
        
        for(int i = minHd; i <= maxHd; i++) {
            Collections.sort(map.get(i), (n1, n2) -> {
                if(n1.verticalDistance == n2.verticalDistance)
                    return n1.treeNode.val - n2.treeNode.val;
                else
                    return n2.verticalDistance - n1.verticalDistance;
            });
            List<Integer> l = new ArrayList<>();    
            for(Node node : map.get(i)) 
                l.add(node.treeNode.val);
            res.add(l);    
        }
        return res;
    }
}

References :

https://www.youtube.com/watch?v=QWbVCqIhTO4

vertical-order-traversal-of-a-binary-tree's People

Contributors

emahtab avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.