![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二叉树
dtwd886
这个作者很懒,什么都没留下…
展开
-
leetcode95——不同的二叉搜索树
题目链接:https://leetcode-cn.com/problems/unique-binary-search-trees-ii/ 思路:首先从根节点进行分析,可能值为i,(1<=i<=n)。根据二叉搜索树的性质,左子树的值可能为1~i-1,右子树的值可能为i+1~n。然后依次递归下去,返回到当前层,则将左右子树的根结点串到当前结点就可以了。 /** * Definition for a binary tree node. * struct TreeNode { * i原创 2020-07-22 10:35:33 · 89 阅读 · 0 评论 -
Leetcode66——对称二叉树
一个树为对称的仅当两棵子树互为镜像,即要满足: 1.子树根节点值相等 2.每个树的右子树都与另一个树的左子树镜像对称。 有递归迭代两种解法: 递归: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), righ原创 2020-05-31 17:19:16 · 115 阅读 · 0 评论 -
杭电3791-二叉搜索树
不知道怎么回事,递归过不了,数组模拟却可以,注释部分为递归代码,为注释部分为数组模拟代码(可以AC),求大神指教。 /*#include #include #include typedef struct k { char data; k *lchild,*rchild; }node; void Build(node *&h,char ch) { if(h==NULL)原创 2016-05-17 21:52:41 · 310 阅读 · 0 评论 -
模板-根据中序,后序建树层次遍历输出
#include #include #include #define maxn 35 using namespace std; int in[maxn],post[maxn]; struct stu { int num; stu *l,*r; }; int n; void build(stu *&s,int a,int b,int aa,int bb) { s=(stu*)malloc(si转载 2016-05-18 20:24:44 · 652 阅读 · 0 评论 -
模板-根据前序中序序列输出后序序列(杭电1710)
#include #include #include #include using namespace std; struct node { int data; node *l,*r; }; int cnt; int inorder[1100],preorder[1100],postorder[1100]; void build(node *&s,int a,int b,int a1,原创 2016-05-18 21:03:33 · 262 阅读 · 0 评论 -
杭电1710(纯C代码)
要用纯C写这道题目,首先要注意结构体的定义,用typedef定义结构体时,结构体里面的指针前面要加struct。 如: 还有就是建树的函数,由于C语言中不能实现用&符来进行指针副本定义,所以在建树函数中应该有返回指针类型的返回值。 如: 具体实现见代码: #include #include #include typedef struct T { int data;原创 2017-07-22 17:05:58 · 362 阅读 · 0 评论