kziomek0000
BAN USERClever but supports only tree example in question. It doesn't support tree like this:
*
* ..............6
* ............/...\
* ...........9.....4
* ........../..\....\
* .........5....1....6
* ..........\......../
* ...........0.... .7
* .............\.......
* ..............11
* ................\
* .................12
* ...................\
* ...................13
* .....................\
* ......................14
*/
Traverse Breadth-First to preserve order for nodes with the same horizontal distance. Horizontal distance is used to find nodes in the same column I.e. horizontal distance for nodes 4, 7 and 12 is 1.
/*
..............6
............/...\
...........9.....4
........../..\....\
.........5....1....6
..........\......../
...........0.... .7
.............\.......
..............11
................\
.................12
...................\
...................13
.....................\
......................14
*/
private void printInVerticalOrder(Node root) {
if (root == null){
return;
}
TreeMap<Integer, List<Integer>> hdToValues = collectHorizontalDistancesToValues(root);
print(hdToValues);
}
private TreeMap<Integer, List<Integer>> collectHorizontalDistancesToValues(Node root) {
TreeMap<Integer, List<Integer>> hdToValues = new TreeMap<>();
LinkedList<HDNode> nodes = new LinkedList<>();
nodes.add(new HDNode(0, root));
while (!nodes.isEmpty()) {
HDNode hdNode = nodes.removeFirst();
hdToValues.putIfAbsent(hdNode.hd, new ArrayList<>());
hdToValues.get(hdNode.hd).add(hdNode.node.value);
if(hdNode.node.left != null){
nodes.add(new HDNode(hdNode.hd - 1, hdNode.node.left));
}
if(hdNode.node.right != null){
nodes.add(new HDNode(hdNode.hd + 1, hdNode.node.right));
}
}
return hdToValues;
}
private void print(TreeMap<Integer, List<Integer>> hdToValues) {
while (!hdToValues.isEmpty()) {
Map.Entry<Integer, List<Integer>> entry = hdToValues.pollFirstEntry();
entry.getValue().forEach(v -> System.out.print(v + " "));
System.out.println("\n");
}
}
private class HDNode {
int hd;
Node node;
HDNode(int hd, Node node) {
this.hd = hd;
this.node = node;
}
}
RepDarshWilliams, abc at 8x8
Exceptionally dedicated and professional Academic Advisor with a superb record of student support and satisfaction. Read about dua to put ...
Reprizzafilher, Java Developer at Achieve Internet
Sorter with 3+ years of experience , performs duties in a safe manner in compliance with all local, state, and federal ...
Repmarthahiggs71, Intern at Achieve Internet
Hi, I am Martha from Toledo, OH. Working with Top NYC consulting engineers by helping them design controls projects. Have ...
Repyalendatopi, Dev Lead at Alliance Global Servies
I am Yalenda , a video editor with 3+ years of experience, skilled in Premiere Pro. Seeking position with Weingarten's ...
Replillymartin, Senior Software Development Engineer at Aristocrat Gaming
Hi everybody! I'm Lilly, a 22 year old girl from Us. Master of economic and financial evaluation of development ...
Repirenereed98, Discount Furniture Showcase at Physical therapist aid
I'm a Physical advisor helping working at Discount Furniture Showcase . I'm likewise a film buff who partakes in ...
Repzk6354367, Financial Software Developer at Agilent Technologies
Hi , I am Zessie from the USA , working as Dien for the last three years. Previously I have served as ...
RepSharonSwann, AT&T Customer service email at A9
Bilingual, self-motivated Air Hostess with a proven record of providing excellent customer service and exceeding all corporate and personal expectations ...
RepDonnaTyler, HR Executive freshers at Bloomberg LP
I am Donna , a travel counselor who advises clients on travel options and tour packages, makes bookings , prepares tickets and ...
RepMillaSoth, Intern at ADP
I am Milla , a dedicated Emergency Response Technician regarded for performing advanced medical procedures with a high degree of accuracy ...
Repjanicepdaniels1, Backend Developer at Accenture
I decided to become an entrepreneur and work for myself because I wasn't making the money I wanted to ...
RepMoniKim, Animator at Clean Power Research
I am Moni , a reliable Special Projects/Order Filler people oriented personality with great organizational and interpersonal communication skills. I ...
RepJoseElkins, Animator at ASU
I am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...
Reproseljannet75, Financial Software Developer at Abs india pvt. ltd.
Je suis jannet , une conseillère en voyages qui conseille les clients sur les options de voyage et les voyages organisés ...
RepNaomiAllen, abc at HUIM
Hard-working, passionate coach who excels at teaching children between the ages of 8 and 16 about the fundamentals of football ...
Repharveyoberion, Analyst at AMD
Hi, I am Harvey, from the USA. I am working as a soil scientist. I study soil as a natural ...
Repseinarondal, Applications Developer at ABC TECH SUPPORT
I am Seina, a hardworking individual with many years of customer service experience seeking a position with a company utilizing ...
Repjoeevansjoe6, Repairer at Monlinks
I have been functioning as a repairman at monlinks organization for a long time . Here I learn numerous things . My ...
Repdelioshorn, Member Technical Staff at Atmel
Attentive Teller Supervisor with 4 years of experience in assisting customers to meet financial needs and referring customers to partners ...
RepDukeRollin, Java Experienced at AMD
Duke, a Qualified psychiatrist with seven years of experience effectively treating patients with a wide range of conditions with a ...
- kziomek0000 April 28, 2019