具体思路:
傻比题,读题都读不明白;
具体代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> boundaryOfBinaryTree(TreeNode* root) {
vector<int>lbonder;
vector<int>rbonder;
vector<int>children;
find_left(root->left, lbonder);
find_right(root->right, rbonder);
find_child(root->left,children);
find_child(root->right, children);
vector<int>ret;
ret.insert(ret.begin(), root->val);
ret.insert(ret.end(),lbonder.begin(),lbonder.end());
ret.insert(ret.end(),children.begin(),children.end());
reverse(rbonder.begin(), rbonder.end());
ret.insert(ret.end(),rbonder.begin(),rbonder.end());
return ret;
}
void find_left(TreeNode* root,vector<int>& vec){
if(!root||(!root->left&&!root->right)){
return;
}
vec.push_back(root->val);
if(root->left){
find_left(root->left, vec);
}else{
find_left(root->right, vec);
}
}
void find_right(TreeNode* root,vector<int>& vec){
if(!root||(!root->left&&!root->right)){
return;
}
vec.push_back(root->val);
if(root->right){
find_right(root->right, vec);
}else{
find_right(root->left, vec);
}
}
void find_child(TreeNode* root,vector<int>& vec){
if(!root)
return;
if(!root->left&&!root->right)
vec.push_back(root->val);
find_child(root->left, vec);
find_child(root->right, vec);
}
};