Given A binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can SE E ordered from top to bottom.
For example:
Given The following binary tree,
1 <---/ 2 3 <---\ 5 4 <---
You should return [1, 3, 4] .
Problem Solving Ideas:
DFS, with a height, Java implementation is as follows:
Public list<integer> Rightsideview (TreeNode root) {list<integer> List = new arraylist<integer> (); if ( root = null) return List;list.add (Root.val), if (root.right! = null) DFS (list, root.right, 1), if (root.left! = null) DFS (LIS T, Root.left, 1); return list;} static void Dfs (List<integer> List, TreeNode root, int height) {if (height = = list.size ()) List.add (root.val); if (roo T.right! = null) DFS (list, root.right, height+1), if (root.left! = null) DFS (list, root.left, height+1);}
Java for Leetcode 199 Binary Tree right Side View