当前位置:网站首页>Insect binary tree OJ
Insect binary tree OJ
2022-04-21 14:21:00 【Hua Weiyun】
Binary tree OJ Quenched body
example 1: Single valued binary trees
subject


bool isUnivalTree(struct TreeNode* root){ // An empty tree is simply a single valued tree if(!root) return true; // If there is only one left node if(root->left && root->left->val != root->val) return false; // If there is only one right node if(root->right && root->right->val != root->val) return false; return isUnivalTree(root->left) && isUnivalTree(root->right);}
example 2: Preorder traversal of two tree
subject


// Let's find out the number of nodes of the binary tree int BinaryTreeSize(struct TreeNode* root){ return root == NULL ? 0 : BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;}void _preorderTraversal(struct TreeNode* root,int* a,int* i){ if(!root) return; a[(*i)++] = root->val; _preorderTraversal(root->left,a,i); _preorderTraversal(root->right,a,i);}int* preorderTraversal(struct TreeNode* root, int* returnSize){ // If we know the number of binary tree nodes, we can open up the array size int size = BinaryTreeSize(root); int* a = (int*)malloc(sizeof(int)*size); // We recurs directly preorderTraversal it , It's not easy to recurse, because every time you recurse, you open up an array , Not reality // We should recurse his functions with similar properties , But you can't open up arrays again and again , You should pass the array to a subscript int i = 0; _preorderTraversal(root,a,&i); *returnSize = size; return a;}
example 3: Middle order traversal of binary trees
subject


// Binary tree node number function int BinaryTreeSize(struct TreeNode* root){ if(!root) return 0; return BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;}// Subfunctions void _inorderTraversal(struct TreeNode* root,int* a,int* pi){ if(!root) return; _inorderTraversal(root->left,a,pi); a[(*pi)++] = root->val; _inorderTraversal(root->right,a,pi); }int* inorderTraversal(struct TreeNode* root, int* returnSize){ // Assign the number of nodes to the array size int size = BinaryTreeSize(root); // Create an appropriate array int* a = (int*)malloc(sizeof(int)*size); int i = 0; _inorderTraversal(root,a,&i); *returnSize = size; return a;}
example 4: Postorder traversal of binary trees
subject


// Binary tree int BinaryTreeSize(struct TreeNode* root){ return root == NULL ? 0 : BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;}// Subfunctions void _postorderTraversal(struct TreeNode* root,int* a,int* pi){ if(!root) return; _postorderTraversal(root->left,a,pi); _postorderTraversal(root->right,a,pi); a[(*pi)++] = root->val;}int* postorderTraversal(struct TreeNode* root, int* returnSize){ // Pass the array size int size = BinaryTreeSize(root); // Create an appropriate array int* a = (int*)malloc(sizeof(int)*size); int i = 0; _postorderTraversal(root,a,&i); *returnSize = size; return a;}
example 5: Same tree
subject


bool isSameTree(struct TreeNode* p, struct TreeNode* q){ // All is empty if(!p && !q) return true; // There is and only one is empty if(!p || !q) return false; // There is no empty tree if(p->val != q->val) return false; return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);}
example 6: Symmetric binary tree
subject


bool _isSymmetricTree(struct TreeNode* root1,struct TreeNode* root2){ // If both are empty, it returns true if(!root1 && !root2) return true; // There is only one empty direct false if(!root1 || !root2) return false; if(root1->val != root2->val) return false; return _isSymmetricTree(root1->left,root2->right) && _isSymmetricTree(root1->right,root2->left);}bool isSymmetric(struct TreeNode* root){ // An empty tree is symmetric if(!root) return true; // Return their judgment return _isSymmetricTree(root->left,root->right);}
example 7: The subtree of another tree
subject


// Determine whether it is the same tree bool isSameTree(struct TreeNode* p, struct TreeNode* q){ // All is empty if(!p && !q) return true; // There is and only one is empty if(!p || !q) return false; // There is no empty tree if(p->val != q->val) return false; return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);}bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){ if(!root) return false; if(isSameTree(root,subRoot)) return true; return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot); }
example 8: Binary tree traversal
subject



#include <stdio.h>#include <stdlib.h>// Tree node typedef struct BinaryTreeNode{ struct BinaryTreeNode* right; struct BinaryTreeNode* left; int val;}BTNode;// Create trees BTNode* CreateTree(char* str,int* pi){ //# Return empty if(str[*pi] == '#') { (*pi)++; return NULL; } // It's not an empty start BTNode* root = (BTNode*)malloc(sizeof(BTNode)); root->val = str[(*pi)++]; // Recursive create root->right = CreateTree(str,pi); root->left = CreateTree(str,pi); return root;}// In the sequence traversal void InOrder(BTNode* root){ // Empty returns if(!root) return; // Go left first InOrder(root->right); // Printing printf("%c ",root->val); // Go right again InOrder(root->left);}int main(){ // Because no more than 100 char str[100] = {0}; // Multi group input while(scanf("%s",str) != EOF) { // Create trees int i = 0; BTNode* root = CreateTree(str,&i); InOrder(root); } return 0;}
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211406369534.html
边栏推荐
- Dynatrace抓取系统中的任何方法Method的参数值
- WordPress博客搭建指南
- Summary of network module knowledge points
- 无人驾驶虚拟仿真(十四)--图像处理之交通标志牌识别2
- 虫子 时空复杂度
- C语言每日一题第一天
- MySQL kills 16 questions. How long can you hold on to it?
- 【错误记录】Groovy工程中的文件查找策略 ( main 函数中需要使用 src/main/groovy/Script.groovy | Groovy 脚本直接使用代码相对路径 )
- 录制你的第一个web 自动化测试用例
- 代码重构之内联函数
猜你喜欢

English word memory method based on anki + vocabulary

堆排序--TOP-K问题解决及复杂度分析

无人驾驶虚拟仿真(十六)--障碍物检测与识别2

Two days and two nights, 1m pictures are optimized to 100kb

带你轻松玩转C语言函数

Redisjson: a redis that can store JSON

A quietly rising domestic software

REST-assured 获取日志到文件并结合 Allure 报告进行展示

Crawler example: climb the ranking of Chinese Universities

如何关闭VS Code eslint校验,快来看看吧
随机推荐
C language selection and circulation classic exercises
虫子 顺序表
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
无人驾驶虚拟仿真(十三)--图像处理之交通标志牌识别1
如何关闭VS Code eslint校验,快来看看吧
Numpy foundation of pytorch machine learning
Monotonicity and concavity of function
面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
初窥 Pytest 测试框架,基础薄弱也能轻松 hold 住
快速排序几种实现方法及其优化
如何在 OpenShift 中运行 Collabora Office
顺序表--链表实现
顺序表例题个人总结
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
Notes on questions (I)
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
技术分享 | Selenium 测试用例编写
Heap sorting -- Top-k problem solving and complexity analysis
无人驾驶虚拟仿真(十四)--图像处理之交通标志牌识别2
MySQL kills 16 questions. How long can you hold on to it?