# My simple accepted solution(JAVA)

• The core idea of this algorithm:

1.Each depth of the tree only select one node.

1. View depth is current size of result list.

Here is the code:

``````public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
rightView(root, result, 0);
return result;
}

public void rightView(TreeNode curr, List<Integer> result, int currDepth){
if(curr == null){
return;
}
if(currDepth == result.size()){
}

rightView(curr.right, result, currDepth + 1);
rightView(curr.left, result, currDepth + 1);

}
}``````

• What is the time complexity?

• It should be O(n) time cost where n is the total node of the tree.

• This post is deleted!

• But this is really elegant and clear code and the algorithm is easy to catch!
Thanks for sharing!

• Thanks a lot!

• This algorithm just traverse each node once. So it's time cost should be indeed O(n). I think the point you wanna mention is the space complexity. The algorithm cost (log(n) ~ n) memory space because of the recursion call used.

• Great reply! Thanks a lot!

• Good ideal! I used bfs method to solve this problem and i think your code is better and more simple

• Thank you very much! I'm full of energy now : )

• ``````    public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
helper(res, root, 0);
return res;
}

public void helper(List<Integer> res, TreeNode root, int level) {
if(root == null) return;
if(res.size() == level)
helper(res, root.right, level + 1);
helper(res, root.left, level + 1);
}
``````

same idea with you, can we do it without recursion??

• Use a stack to simulate the process of recursion. I will put the code after your answer.

• Using a stack to simulate this algorithm instead of recursion:

``````public class Solution {
public List<Integer> rightSideView(TreeNode root) {
if(root == null){
return new ArrayList<Integer>();
}

List<Integer> result = new ArrayList<Integer>();
Stack<NodeWithDep> stack = new Stack<NodeWithDep>();

NodeWithDep nwdRoot = new NodeWithDep(root, 0);
stack.push(nwdRoot);
while(!stack.isEmpty()){
NodeWithDep nwd = stack.pop();
TreeNode curr = nwd.node;
if(nwd.depth == result.size()){
}
if(curr.left != null){
stack.push(new NodeWithDep(curr.left, nwd.depth + 1));
}
if(curr.right != null){
stack.push(new NodeWithDep(curr.right, nwd.depth + 1));
}
}
return result;
}

class NodeWithDep{
TreeNode node;
int depth;
NodeWithDep(TreeNode node, int depth){
this.node = node;
this.depth = depth;
}
}

}``````

• Great DFS code! Very nice idea of noticing `currDepth = result.size()` to be the condition to add the node.

• The recursive code is much harder. But your code is still very clear :-)

• Thanks for your approval !

• Again, Thank you !

• I don't understand why this answer becomes top 1. The binary level order traversal (i.e., BFS solution) is easier to understand and has the same time/space efficiency.

• This post is deleted!

• Try something hard to improve skills.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.