# switch - java while

## 嘗試使用兩個if語句來打印樹的頂視圖 (10)

``````void top_view(Node root)
{
Node r = root;

if(r.left!=null){
top_view(r.left);
System.out.print(r.data + " ");
}
if(r.right!=null){
System.out.print(r.data + " ");
top_view(r.right);
}
}``````

（最後看代碼，非常簡單）

``````    horizontal distance of 3 = 0

horizontal distance of 5 = -1
horizontal distance of 1 = -2
horizontal distance of 9 = -1
horizontal distance of 4 = 0

horizontal distance of 2 =  1
horizontal distance of 6 =  0
horizontal distance of 7 =  2
horizontal distance of 8 =  0``````

``````hor dist        Nodes

0      - {3,6,8} // 3 comes first in BFS so print 3
-1      - {5,9}   // 5 comes first in BFS so print 5
-2      - {1}     // just print 1
1      - {2}     // just print 2
2      - {7}     // just print 7``````

``````HashSet<Integer> set = new HashSet<>();
queue.add(new QueueItem(root, 0)); // Horizontal distance of root is 0

while (!queue.isEmpty())
{
QueueItem temp = queue.poll();
int hd = temp.hd;
TreeNode n = temp.node;

// If this is the first node at its horizontal distance,
// then this node is in top view
if (!set.contains(hd))
{
System.out.print(n.key + " ");
}

if (n.left != null)
if (n.right != null)
}``````

`````` void for_left(node *root)
{
if(!root->left)
{
cout<<root->data<<" ";
return;
}
for_left(root->left);
cout<<root->data<<" ";
return;

}

void top_view(node * root)
{
for_left(root->left);
cout<<root->data<<" ";
while(root->right)
{
cout<<(root->right)->data<<" ";
root=root->right;
}

}``````

``````void top_view(Node root)
{
if(root.left!=null) top_view(root.left);

if(root.left!=null || root.right!=null)
System.out.print(root.data + " ");

if(root.right!=null) top_view(root.right);
}``````

C ++中一個更簡單的方法

```````// printing top view of the tree
void left_array(node *p)
{
if(p==NULL)
return;
else
{
left_array(p->left);
cout<<p->data<<" ";
}
}
void right_array(node *p)
{
if(p==NULL)
return;
else
{
cout<<p->data<<" ";
right_array(p->right);
}

}
void top_view(node * root)
{   int i=0;
node *t1=root;
node *t2=root;
left_array(t2);
right_array(t1->right);

}```````

`````` void topview(Node root)
{
if(root==null)
return;
Stack<Integer> s=new Stack<Integer>();
s.push(root.data);
Node root2=root;
while(root.left!=null)
{
s.push(root.left.data);
root=root.left;
}
while(s.size()!=0)
System.out.print(s.pop()+" ");

root2=root2.right;
while(root2.right!=null)
{
root2=root2.right;
}
while(q.size()!=0)
System.out.print(q.poll()+" ");
}``````

``````void top_view(Node root)
{
Stack<Node> p = new Stack<Node>();
Node current = root;
while (current != null)
{
p.push(current);
current = current.left;
}

while (p.peek() != root)
{
System.out.print(p.pop().data + " ");
}

current = root;
while (current != null)
{
System.out.print(current.data + " ");
current = current.right;
}
}``````

``````void top_view(Node root)
{
Node left = root;
Node right = root;
print_left(root.left);
System.out.print(root.data + " ");
print_right(root.right) ;
}

void print_left(Node start)
{
if(start != null)
{
print_left(start.left);
System.out.print(start.data + " ");
}
}

void print_right(Node start)
{
if(start != null)
{
System.out.print(start.data + " ");
print_right(start.right);
}
} ``````

``````void top_view(Node root)
{
print_top_view(root.left, "left");
System.out.print(root.data  + " ");
print_top_view(root.right, "right");
}

void print_top_view(Node root, String side) {
if(side.equals("left")) {
if(root.left != null) {
print_top_view(root.left, "left");
}
System.out.print(root.data + " ");
} else if(side.equals("right")) {
System.out.print(root.data + " ");
if(root.right != null) {
print_top_view(root.right, "right");
}
}
}``````

void topview（node * root，queue＆Q）

{

``````if(!root)
return;
map<int,int> TV;
Q.push(root);
TV[root->data]=0;
map<int,int>:: iterator it;
int min=INT_MAX,max=INT_MIN;
while(!Q.empty())
{
node* temp =Q.front();
Q.pop();
int l=0;

for(it=TV.begin();it!=TV.end();it++)
{
if(it->first==temp->data)
{
l=it->second;
break;
}

}
if(l<min)
{min=l;}
if(l>max)
max=l;
if(temp->left)
{
Q.push(temp->left);
TV[temp->left->data] = l-1;
}
if(temp->right)
{
Q.push(temp->right);
TV[temp->right->data] = l+1;
}
}
cout<<max<<min<<endl;
for(int  i =min;i<=max;i++)
{
for(it=TV.begin();it!=TV.end();it++)
{
if(it->second==i)
{
cout<<it->first;
break;
}
}
}``````

}

void topview_aux（node * root）

{

``````queue<node*> Q;

topview(root,Q);``````

}

``````void top_view(Node root)
{
// For left side of the tree
top_view_left(root);
// For Right side of the tree
top_view_right(root.right);
}

void top_view_left(Node root){
if(root != null)
{
// Postorder
top_view_left(root.left);
System.out.print(root.data + " ");
}
}

void top_view_right(Node root){
if(root != null)
{
//  Preorder
System.out.print(root.data + " ");
top_view_right(root.right);
}
}``````