void printShittyNodes(TreeNode *r, int toFind)
{
if (r != null)//node不为空的情况下
{
if ((r->key) >= toFind)//如果parent node大于n ,左右node的key都大于n,因此要同时访问2边
{
cout << key << endl;
printShittyNodes(r->left, toFind);
printShittyNodes(r->right, toFind);
} else { //如果parent node小于n
//只有当右node不为empty node, and the key of the right node is greater than n
if ((r->right) != null) //we have to separate these 2 condition, a seq. fault might occur
if ((r->right)->key) > toFind)
printShittyNodes(r->right, toFind);
}
}
}
最初由 风中有影 发布
void TreeNode::subprint (TreeNode *subRoot,int n) {
if(subRoot==NULL) return;
if(subRoot->key>=n){
subPrint (subRoot -> left,n);
cout << subRoot -> key << endl;
subPrint (subRoot -> right,n);
}
if(subRoot->right!=NULL&&subRoot->right->key>n){
subPrint (subRoot -> right,n);
}
else{
return;
}
}
Basic case:
subRoot == NULL, 说明没找到比n大的,就return。
subRoot->key > n 说明subRoot比n大,按题目要求应该print。
Recursive process:
因为subRoot比n大,所以check 左面的tree,右面的tree其实不用check 直接print就好了。
如果subRoot->key < n, 说明你要找的key不可能在左边的tree里,所以直接check 右面。