## Tuesday, April 26, 2016

### LeetCode 199 - Binary Tree Right Side View

https://leetcode.com/problems/binary-tree-right-side-view/
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]`.
X. Level Order Traverse
Obviously, the question asks for a BFS. The major difference from the traditional BFS is it only prints the right-most nodes for each level. Consequently, the basic idea is to add right child before adding the left child. For each level, only print out the first node, which must be the right-most node.
http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/
This problem can be solve by using a queue. On each level of the tree, we add the right-most element to the results.
``` public List<Integer> rightSideView(TreeNode root) { ArrayList<Integer> result = new ArrayList<Integer>(); if(root == null) return result; LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root);   while(queue.size() > 0){ //get size here int size = queue.size();   for(int i=0; i<size; i++){ TreeNode top = queue.remove();   //the first element in the queue (right-most of the tree) if(i==0){ result.add(top.val); } //add right first if(top.right != null){ queue.add(top.right); } //add left if(top.left != null){ queue.add(top.left); } } }   return result; } ```
public List<Integer> rightSideView(TreeNode root) { List<Integer> res = new LinkedList<Integer>(); if(root == null) return res; List<TreeNode> candidates = new LinkedList<TreeNode>(); candidates.add(root); while(!candidates.isEmpty()) { List<TreeNode> temp = new LinkedList<TreeNode>(); res.add(candidates.get(0).val); for(TreeNode curr : candidates) { if(curr.right != null) temp.add(curr.right); if(curr.left != null) temp.add(curr.left); } candidates = temp; } return res; }
https://leetcode.com/discuss/30464/reverse-level-order-traversal-java
But why bother to use reverse level order traversal? :)
public List<Integer> rightSideView(TreeNode root) { List<Integer> result = new ArrayList<>(); if (root == null) { return result; } Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); if (i == size - 1) { // last element in current level result.add(node.val); } if (node.left != null) { queue.add(node.left); } if (node.right != null) { queue.add(node.right); } } } return result; }
X. DFS
http://buttercola.blogspot.com/2015/04/leetcode-binary-tree-right-side-view.html
https://leetcode.com/discuss/65483/simple-java-solution-w-recursion-2ms
`    ``public` `List<Integer> rightSideView(TreeNode root) {`
`        ``List<Integer> result = ``new` `ArrayList<Integer>();`
`        `
`        ``if` `(root == ``null``) {`
`            ``return` `result;`
`        ``}`
`        `
`        ``rightSideViewHelper(root, ``0``, result);`
`        `
`        ``return` `result;`
`    ``}`
`    `
`    ``private` `void` `rightSideViewHelper(TreeNode root, ``int` `level, List<Integer> result) {`
`        ``if` `(root == ``null``) {`
`            ``return``;`
`        ``}`
`        `
`        ``if` `(level == result.size()) {`
`            ``result.add(root.val);`
`        ``}`
`        `
`        ``rightSideViewHelper(root.right, level + ``1``, result);`
`        ``rightSideViewHelper(root.left, level + ``1``, result);`
`    ``}`
https://leetcode.com/discuss/45254/java-solution-of-10-lines-code-dfs
public List<Integer> rightSideView(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); help(root,1,result); return result; } public void help(TreeNode root, int depth, List<Integer> result){ if(root==null) return; if(result.size()<depth) result.add(root.val); help(root.right,depth+1,result); help(root.left,depth+1,result); }
https://leetcode.com/discuss/40084/java-solution-using-recursion
Comments: height == result.size() is the core part in this recursion, it limits the amount of Node add to the result in each level(height) of the Tree.
Some thoughts: If the questions is asking for a left view of the binary tree, just swap the order of
``````if (root.right != null) {
helper(root.right, result, height + 1);

}
``````
and
``````if (root.left != null) {
helper(root.left, result, height + 1);
}
``````
Moreover, if it's asking of the "x-ray view" of the binary tree, for example, display the second element from the right view(given a valid tree). The solution could be adding a counter inside
``````if (height == result.size()) {
}
``````
and keep track of the counter.
https://leetcode.com/discuss/33021/recursive-solution
public List<Integer> rightSideView(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); if(root == null) return res; visitLevel(root, 1, res); return res; } public void visitLevel(TreeNode root, int level, List<Integer> res){ if(root == null) return; if(level > res.size()){ res.add(root.val); } visitLevel(root.right, level+1, res); visitLevel(root.left, level+1, res); }
X. Different way - not efficient
https://leetcode.com/discuss/30445/java-solution-using-divide-and-conquer
Worst case is O(n^2), so it's very slow. Think about a tree with only one node at every level.