Right view of Binary tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void dfs(TreeNode* node, int depth, vector<int>& res) { if(!node) return; if(res.size() == depth) res.push_back(-1); res[depth] = node->val; dfs(node->left, depth + 1, res); dfs(node->right, depth + 1, res); } vector<int> Solution::solve(TreeNode* A) { vector<int> res; dfs(A,0,res); return res; }
|