null
作者:
海里的枕头
,
2024-09-28 08:51:20
,
所有人可见
,
阅读 3
王道P205
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> a;
typedef struct Node{
int data;
struct Node *l, *r;
}BNode, *BTree;
void inorder(BTree p, int sum, vector<int> &a){
if(!p) return;
int x = p -> data;
a.push_back(x);
if(! p -> l && ! p -> r){
if(sum + x == 22){
for(int i = 0; i < a.size(); i ++) printf("%d ", a[i]);
puts("");
}
}
inorder(p -> l, sum + x, a);
inorder(p -> r, sum + x, a);
a.pop_back();
}
int main(){
BNode *p = new BNode[20];
p -> data = 10, p -> l = (p + 1), p -> r = (p + 2);
(p + 1) -> data = 5, (p + 1) -> l = (p + 3), (p + 1) -> r = (p + 4);
(p + 2) -> data = 12, (p + 2) -> l = NULL, (p + 2) -> r = NULL;
(p + 3) -> data = 4, (p + 3) -> l = NULL, (p + 3) -> r = NULL;
(p + 4) -> data = 7, (p + 4) -> l = NULL, (p + 4) -> r = NULL;
inorder(p, 0, a);
return 0;
}