Given the root of 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.
Example 1:
Input:root = [1,2,3,null,5,null,4]
Output:[1,3,4]
Explanation:
Example 2:
Input:root = [1,2,3,4,null,null,null,5]
Output:[1,3,4,5]
Explanation:
Example 3:
Input:root = [1,null,3]
Output:[1,3]
Example 4:
Input:root = []
Output:[]
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
Solutions
Solution 1: BFS
We can use breadth-first search (BFS) and define a queue $\textit{q}$ to store the nodes. We start by putting the root node into the queue. Each time, we take out all the nodes of the current level from the queue. For the current node, we first check if the right subtree exists; if it does, we put the right subtree into the queue. Then, we check if the left subtree exists; if it does, we put the left subtree into the queue. This way, the first node taken out from the queue each time is the rightmost node of that level.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 9101112131415161718192021
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:defrightSideView(self,root:Optional[TreeNode])->List[int]:ans=[]ifrootisNone:returnansq=deque([root])whileq:ans.append(q[0].val)for_inrange(len(q)):node=q.popleft()ifnode.right:q.append(node.right)ifnode.left:q.append(node.left)returnans
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcrightSideView(root*TreeNode)(ans[]int){ifroot==nil{return}q:=[]*TreeNode{root}forlen(q)>0{ans=append(ans,q[0].Val)fork:=len(q);k>0;k--{node:=q[0]q=q[1:]ifnode.Right!=nil{q=append(q,node.Right)}ifnode.Left!=nil{q=append(q,node.Left)}}}return}
Use DFS (depth-first search) to traverse the binary tree. Each time, traverse the right subtree first, then the left subtree. This way, the first node visited at each level is the rightmost node of that level.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 910111213141516171819
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:defrightSideView(self,root:Optional[TreeNode])->List[int]:defdfs(root:Optional[TreeNode],depth:int)->None:ifrootisNone:returniflen(ans)==depth:ans.append(root.val)dfs(root.right,depth+1)dfs(root.left,depth+1)ans=[]dfs(root,0)returnans
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcrightSideView(root*TreeNode)(ans[]int){vardfsfunc(*TreeNode,int)dfs=func(root*TreeNode,depthint){ifroot==nil{return}iflen(ans)==depth{ans=append(ans,root.Val)}dfs(root.Right,depth+1)dfs(root.Left,depth+1)}dfs(root,0)return}