1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
二叉树前中后序搜索.cpp 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#include <iostream>
using namespace std;
typedef struct Node{
int data;
Node * left;
Node * right;
} Node;
int f(Node* node, int t){
int n;
// left child
if(node->left != NULL) n = f(node->left,t);
if(n == 1) return 1;
// right child
if(node->right != NULL) n = f(node->right,t);
if(n == 1) return 1;
// root
cout<<"check " << node->data << "......" << endl;
if(node->data == t) return 1;
return 0;
}
int f2(Node* node, int t){
int n;
// root
cout<<"check " << node->data << "......" << endl;
if(node->data == t) return 1;
// left child
if(node->left != NULL) n = f2(node->left,t);
if(n == 1) return 1;
// right child
if(node->right != NULL) n = f2(node->right,t);
if(n == 1) return 1;
return 0;
}
Node* createNode(int d){
Node* temp = new Node();
temp->data = d;
return temp;
}
int main(){
Node* root = createNode(50);
root->left = createNode(25);
root->right = createNode(75);
root->left->left = createNode(12);
root->left->right = createNode(37);
root->right->left = createNode(62);
root->right->right = createNode(88);
cout<<"=======================finding 60======================="<<endl;
if(f2(root, 60)==1) cout<<"found!";
else cout<<"NOT found!"<<endl<<endl;
cout<<"=======================finding 37======================="<<endl;
if(f2(root, 37)==1) cout<<"found!";
else cout<<"NOT found!";
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385