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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #include<iostream> #include<vector> #include<cstring> #include<cstdio> using namespace std;
struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x): val(x), left(NULL), right(NULL){} };
int rootval;
void insertNode(TreeNode* root, int val){ if(!root) return; if(root->val > val){ if(root->left){ insertNode(root->left,val); }else{ TreeNode* t = new TreeNode(val); root->left = t; } }else if(root->val < val){ if(root->right){ insertNode(root->right,val); }else{ TreeNode* t = new TreeNode(val); root->right = t; } } }
bool isContainNode(TreeNode* root, int val){ if(!root) return false; if(root->val == val) return true; if(root->val > val) return isContainNode(root->left,val); else return isContainNode(root->right,val); }
void printTree(TreeNode* root){ if(!root) return; cout << root->val <<""; printTree(root->left); printTree(root->right); }
bool isRoot(int val){ return val == rootval; }
TreeNode* parent(TreeNode *root, int child){ if(!isContainNode(root,child) || root->val == child) return NULL; TreeNode* res = NULL; while(1){ if(!root) return NULL; if((root->left && root->left->val == child) || (root->right && root->right->val == child)){ res = root; break; }else if(root->val > child){ root = root->left; }else if(root->val < child){ root = root->right; } } return res; }
bool isSibling(TreeNode* root, int valA, int valB){ if(!root) return false; if(valA == root->val || valB == root->val) return false; TreeNode* t = parent(root, valA); if(t){ if(t->left && t->left->val == valA ){ return t->right && t->right->val == valB; } else if(t->right && t->right->val == valA ){ return t->left && t->left->val == valB; }else{ return false; } } return false; }
int getDepth(TreeNode* root, int val){ if(root->val == val) return 0; if(root->val > val) return 1 + getDepth(root->left,val); else return 1 + getDepth(root->right,val); }
bool isSameLevel(TreeNode* root, int A, int B){
return isContainNode(root,A) && isContainNode(root,B) && getDepth(root, A) == getDepth(root, B); }
void pirntMassage(bool info){ if(info){ cout << "Yes" << endl; }else{ cout << "No" <<endl; } }
bool isParent(TreeNode* root, int parentVal, int childVal){ if(parent(root, childVal) == NULL) return false; return parentVal == parent(root, childVal)->val; } bool isLeftChild(TreeNode* root, int parentVal, int leftChildVal){ if(parent(root, leftChildVal) == NULL) return false; TreeNode* p = parent(root, leftChildVal); return parentVal == p->val && p->left->val == leftChildVal; }
bool isRightChild(TreeNode* root, int parentVal, int rightChildVal){ if(parent(root, rightChildVal) == NULL) return false; TreeNode* p = parent(root, rightChildVal); return parentVal == p->val && p->right->val == rightChildVal; }
int main(){ int c; cin >> c; if(c <= 0) return 0; cin >> rootval; TreeNode* root = new TreeNode(rootval); for(int i = 1; i < c; i++){ int a; cin >> a; insertNode(root, a); } cin >> c; getchar(); int rootV,siblingA,siblingB, parentv, childv,leftchildv,rightchildv,samelevelA,samelevelB; for(int i = 0 ; i < c; i++){ char state[100]; int q = 0; char ch; while((ch = getchar()) != '\n') state[q++] = ch; state[q] = '\0'; if(strstr(state,"root")){ sscanf(state,"%d is the root",&rootV); pirntMassage(isRoot(rootV)); }else if(strstr(state,"siblings")){ sscanf(state,"%d and %d are siblings",&siblingA,&siblingB); pirntMassage(isSibling(root,siblingA,siblingB)); }else if(strstr(state,"same")){ sscanf(state,"%d and %d are on the same level",&samelevelA,&samelevelB); pirntMassage(isSameLevel(root,samelevelA,samelevelB)); }else if(strstr(state,"parent")){ sscanf(state,"%d is the parent of %d",&parentv,&childv); pirntMassage(isParent(root,parentv,childv)); }else if(strstr(state,"left")){ sscanf(state,"%d is the left child of %d",&leftchildv,&parentv); pirntMassage(isLeftChild(root,parentv,leftchildv)); }else if(strstr(state,"right")){ sscanf(state,"%d is the right child of %d",&rightchildv,&parentv); pirntMassage(isRightChild(root,parentv,rightchildv)); } }
return 0; }
|