Google Interview Question
Software EngineersCountry: United States
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class PrintTreePathWithHyphen2 {
static class Node{
Character c;
Node l,r;
Node(Character c){
this.c = c;
this.l=null;
this.r = null;
}
}
static String GetStringForHyphen(int count){
String hyphenString = "";
for (int i=0; i<count; i++){
hyphenString+="-";
}
return hyphenString;
}
static void PrintTree(Node head, int hyphenCnt, int minHyphenCnt, ArrayList<Node> q, HashMap<Node, Integer> map){
map.put(head, hyphenCnt);
q.add(head);
if (head.r == null && head.l == null){
for (int i=0; i<q.size(); i++){
Node n = q.get(i);
String hyphenStr = GetStringForHyphen(map.get(n) - minHyphenCnt);
System.out.println(hyphenStr + n.c);
}
System.out.println("");
q.remove(q.size()-1);
return;
}
if (head.l != null) {
PrintTree(head.l, hyphenCnt-1, Integer.min(minHyphenCnt, hyphenCnt-1), q, map);
}
if (head.r != null) {
PrintTree(head.r, hyphenCnt+1, Integer.min(minHyphenCnt, hyphenCnt+1), q, map);
}
q.remove(q.size()-1);
}
static public void main(String[] val){
Node head = new Node('A');
head.l = new Node('B');
head.r = new Node('C');
head.l.l = new Node('D');
head.l.r = new Node('E');
head.r.l = new Node('F');
head.r.r = new Node('G');
head.l.l.l = new Node('H');
head.l.l.r = new Node('I');
head.l.r.l = new Node('J');
head.l.r.r = new Node('K');
head.l.r.r.l = new Node('P');
head.l.r.r.l.l = new Node('Q');
head.l.r.r.l.l.l = new Node('R');
head.l.r.r.l.l.l.l = new Node('S');
head.r.l.l = new Node('L');
head.r.l.r = new Node('M');
head.r.r.l = new Node('N');
head.r.r.r = new Node('O');
ArrayList<Node> stack = new ArrayList<Node>();
PrintTree(head, 0, 0, stack, new HashMap<Node, Integer>());
/*
A
B C
D E F G
H I J K L M N O
*/
}
}
share my java solution!
public static void printTreePath(TreeNode root){
List<int[]> path = new ArrayList<>();
printTreePathHelper(root,path,0,0);
}
public static void printTreePathHelper(TreeNode root,List<int[]> path,int pos,int left_most){
path.add(new int[]{root.val,pos});
if(root.left==null && root.right==null){
List<String> ans = new ArrayList<>();
for(int[] ele:path){
String temp = "";
int count = ele[1]-left_most;
for(int i=0;i<count;i++){
temp+="_";
}
temp+=ele[0];
ans.add(temp);
}
for(String str:ans){
System.out.println(str);
}
System.out.println();
}else{
if(root.left!=null){
printTreePathHelper(root.left,path,pos-1,Math.min(left_most,pos-1));
}
if(root.right!=null){
printTreePathHelper(root.right,path,pos+1,left_most);
}
}
path.remove(path.size()-1);
}
#include <iostream>
#include <vector>
using namespace std;
class Node
{
public:
Node(int val) : val_(val)
{
left_ = right_ = NULL;
}
int val_;
Node *left_, *right_;
};
class Entry
{
public:
Entry(Node* n, int pos) : n_(n), pos_(pos) {}
Node *n_;
int pos_;
};
void Print(Node* n, Node* leaf, vector<Entry>& path, int min_pos = 0, int pos = 0)
{
if (n)
{
min_pos = min(min_pos, pos);
path.push_back(Entry(n, pos));
if (n == leaf)
{
for (Entry& e : path)
{
cout << string(e.pos_ - min_pos, '_') << e.n_->val_ << "\n";
}
return;
}
Print(n->left_, leaf, path, min_pos, pos - 1);
Print(n->right_, leaf, path, min_pos, pos + 1);
path.pop_back();
}
}
int main()
{
/*
(1)
/
(2)
\
(4)
\
(3)
\
(7)
/ \
(5) (8)
*/
Node n1(1);
Node n2(2);
Node n3(3);
Node n4(4);
Node n5(5);
Node n6(6);
Node n7(7);
Node n8(8);
n1.left_ = &n2;
n2.right_ = &n4;
n4.right_ = &n3;
n3.right_ = &n7;
n7.left_ = &n5;
n7.right_ = &n8;
vector<Entry> path;
Print(&n1, &n5, path);
return 0;
}
import java.util.ArrayList;
public class PrintTreePathWithHyphen {
static class Node{
Character c;
Node l,r;
Node(Character c){
this.c = c;
this.l=null;
this.r = null;
}
}
static void PrintList(ArrayList<String> list){
System.out.println("Output:");
for (String s: list){
System.out.println(s);
}
System.out.println();
}
static void AddHyphenPrefix(ArrayList<String> list){
for (int j=list.size()-1; j>=0; j--){
String s = list.get(j);
s ="-" + s;
list.set(j, s);
}
}
static String GetStringForHyphen(int count){
String hyphenString = "";
for (int i=0; i<count; i++){
hyphenString+="-";
}
return hyphenString;
}
static void PrintTree(Node node, ArrayList<String> list, int hyphenCount){
if (node.l == null && node.r == null){
PrintList(list);
return;
}else{
if (node.r != null){
list.add(GetStringForHyphen(hyphenCount+1) + node.r.c);
PrintTree(node.r, list, hyphenCount+1);
list.remove(list.size()-1);
}
int hyphenCountL = hyphenCount;
if (node.l != null){
if (hyphenCountL > 0) {
hyphenCountL--;
list.add(GetStringForHyphen(hyphenCountL) + node.l.c);
PrintTree(node.l, list, hyphenCountL);
list.remove(list.size()-1);
} else {
ArrayList<String> listL = (ArrayList<String>)list.clone();
AddHyphenPrefix(listL);
listL.add("" + node.l.c);
PrintTree(node.l, listL, hyphenCountL);
}
}
}
}
static public void main(String[] val){
Node node = new Node('A');
node.l = new Node('B');
node.r = new Node('C');
node.l.l = new Node('D');
node.l.r = new Node('E');
node.r.l = new Node('F');
node.r.r = new Node('G');
node.l.l.l = new Node('H');
node.l.l.r = new Node('I');
node.l.r.l = new Node('J');
node.l.r.r = new Node('K');
node.r.l.l = new Node('L');
node.r.l.r = new Node('M');
node.r.r.l = new Node('N');
node.r.r.r = new Node('O');
ArrayList<String> list = new ArrayList<String>();
list.add("" + node.c);
PrintTree(node, list, 0);
/*
A
B C
D E F G
H I J K L M N O
*/
}
}
- dmitry.labutin March 10, 2017