https://leetcode.com/problems/binary-tree-right-side-view/
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/
https://leetcode.com/discuss/52457/java-solution-myself-asked-question-amazon-phone-interview
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
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
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,
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return
题目是两道,一道LC199,然后问time和space complexity,Follow up 是multple children tree instead of binary tree。另一道LC88,只能iterate 1次。运气不错。[1, 3, 4]
. public void DFS(TreeNode root, int h, List<Integer> view) {
if (root == null) return;
if (view.size() < h + 1)
view.add(root.val);
DFS(root.right, h + 1, view);
DFS(root.left, h + 1, view);
}
public List<Integer> rightSideView(TreeNode root) {
List<Integer> view = new ArrayList<Integer>();
DFS(root, 0, view);
return view;
}
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) { 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);
}
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()) {
result.add(root.val);
}
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
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.