# LeetCode199 打印树最右边的值

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

1            <---
/   \
2     3         <---
\     \
5     4       <---

You should return [1, 3, 4].

/**
* Created by kyle on 2016/5/31.
*/
public class RIghtSideTree {

static   public List<Integer> rightSideView(TreeNode root) {
if(root == null){
return  new ArrayList<Integer>();
}
TreeNode currentTreeNode = new TreeNode(0);
List<Integer> list = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
int cur =0, last =1;
while(queue.size() != 0){
while(cur < last){
currentTreeNode = queue.remove();
if(currentTreeNode.left != null){
}
if(currentTreeNode.right != null){
}
cur++;
}

cur =0;
last = queue.size();
}
return  list;
}

static  TreeNode insertTree(TreeNode root, int val){
if(root == null){
root = new TreeNode(val);
return root;
}

if(root.val >val){
if(root.left == null){
root.left = new TreeNode(val);
}else {
insertTree(root.left, val);
}
}else{
if(root.right == null){
root.right = new TreeNode(val);
}else {
insertTree(root.right, val);
}

}
return  root;
}

static void showTree(TreeNode root){
if(root == null){
// System.out.println("[]");
return;
}

showTree(root.left);
System.out.println(root.val);
showTree(root.right);
}
static  public  void main(String[] args){
TreeNode tr;
tr = null;
tr = insertTree(tr, 18);
tr = insertTree(tr, 5);
tr = insertTree(tr, 2);
tr = insertTree(tr, 8);
tr = insertTree(tr, 81);
tr = insertTree(tr, 101);
System.out.println("Original:\n");
showTree(tr);
List<Integer> list = rightSideView(tr);
System.out.println("side way");
System.out.println(list);
}
}

02-24 890

04-17 565

08-08 1463

04-01 1108

08-05 568

03-11 770

05-30 21

08-25 1871

05-30 23

08-02 1168