1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
class Solution { public: vector<int> boundaryOfBinaryTree(TreeNode* root) { if (!root) return {}; l.push_back(root->val); helperL(root->left, true); helperR(root->right, true); l.insert(end(l), rbegin(r), rend(r)); return l; }
void helperL(TreeNode *root, bool border) { if (!root) return; if (border) { l.push_back(root->val); helperL(root->left, true); helperL(root->right, !root->left); } else if (!root->left && !root->right) { l.push_back(root->val); } else { helperL(root->left, border); helperL(root->right, border); } }
void helperR(TreeNode *root, bool border) { if (!root) return; if (border) { r.push_back(root->val); helperR(root->right, true); helperR(root->left, !root->right); } else if (!root->left && !root->right) { r.push_back(root->val); } else { helperR(root->right, border); helperR(root->left, border); } }
vector<int> l, r; };
|