void rightSideView(TreeNode* root, vector<vector<int>> &vec, int depth) { if(!root) return ; if(depth == vec.size()) vec.push_back(vector<int>()); vec[depth].push_back(root->val); rightSideView(root->left, vec, depth+1); rightSideView(root->right, vec, depth+1); }
void rightSideView(TreeNode* root, vector<int>& v, int level) { if(!root) return; if(v.size() == level) v.push_back(root->val); rightSideView(root->right, v, level+1); rightSideView(root->left, v, level+1); }
void rightSideView(TreeNode *root, int l) { if(!root) return; if(l == level + 1) { result.push_back(root->val); level++; } rightSideView(root->right, l+1); rightSideView(root->left, l+1); }
vector<int> rightSideView(TreeNode* root) { vector<int> result; result.resize(0); if (root == NULL) return result; result.push_back(root->val); vector<int> left = rightSideView(root->left); vector<int> right = rightSideView(root->right); for (vector<int>::iterator iter = right.begin(); iter != right.end(); ++iter) { result.push_back(*iter); } if (left.size() > right.size()) { for (int i = right.size(); i < left.size(); ++i) { result.push_back(left[i]); } } return result; }
vector<int> rightSideView(TreeNode* root) { if(!root) return result; level = 1; result.push_back(root->val); rightSideView(root, level); return result; }
vector<int> rightSideView(TreeNode* root) { vector<int> v; if(!root) return v; vector<vector<int>> vec; rightSideView(root, vec, 0); for(int i=0; i<vec.size(); i++) { v.push_back(vec[i][vec[i].size()-1]); } return v; }
int main(){ TreeNode t1 , t2, t3 , t4, t5, t6, t7; t1.val = 1; t2.val = 2; t3.val = 3; t4.val = 4; t5.val = 5; t6.val = 6; t7.val = 7; t1.left = &t2; t1.right = &t3; t2.right = &t5; t3.right = &t4; t5.left = &t6; t5.right = &t7; int *res, *n; n = (int*)malloc(sizeof(int)); res = rightSideView(&t1, n); printf("%d\n", *n); for(int i = 0; i < *n; i++) printf("%d\n", res[i]); printf("\n"); }
int main() { struct TreeNode nodes[] = { {1, NULL, NULL}, {2, NULL, NULL}, {3, NULL, NULL} }; int i, returnSize; nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; int *result = rightSideView(&nodes[0], &returnSize); for (i = 0; i < returnSize; i++) { printf("%d\n", result[i]); } free(result); }
vector<int> rightSideView(TreeNode* root) { vector<int> result; rightSideView(root, result, 0); return result; }