Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- vector<int> Solution::solve(TreeNode* A, int B) {
- queue<TreeNode*> q;
- q.push(A);
- vector<int>vec;
- while(!q.empty()){
- int n = q.size();
- bool found = false;
- while(n--){
- TreeNode* x = q.front();
- q.pop();
- if((x->left && x->left->val == B) ||(x->right && x->right->val == B))
- found = true;
- else{
- if(x->left)
- q.push(x->left);
- if(x->right)
- q.push(x->right);
- }
- }
- if(found){
- while(!q.empty()){
- vec.push_back(q.front()->val);
- q.pop();
- }
- break;
- }
- }
- return vec;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement