赞
踩
目录
OJ题链接https://leetcode.cn/problems/univalued-binary-tree/
思路:
代码:
- bool isUnivalTree(struct TreeNode* root)
- {
- if(root==NULL)
- return true;
- if(root->left && root->left->val != root->val)
- return false;
- if(root->right && root->right->val != root->val)
- return false;
- return isUnivalTree(root->left) && isUnivalTree(root->right);
- }
OJ题链接https://leetcode.cn/problems/same-tree/
思路:
注意:
代码:
- bool isSameTree(struct TreeNode* p, struct TreeNode* q)
- {
- if(p==NULL && q==NULL)
- return true;
-
- if(p==NULL || q==NULL)
- return false;
-
- if(p->val != q->val)
- return false;
-
- return isSameTree(p->left,q->left) && isSameTree(p->right, q->right);
- }
OJ题链接https://leetcode.cn/problems/subtree-of-another-tree/
思路:
代码:
- bool isSameTree(struct TreeNode* p, struct TreeNode* q)
- {
- if(p==NULL && q==NULL)
- return true;
-
- if(p==NULL || q==NULL)
- return false;
-
- 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==NULL)
- return false;
-
- if(isSameTree(root,subRoot))
- return true;
-
- return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);
- }
OJ题链接https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
思路:
代码:
- struct TreeNode* invertTree(struct TreeNode* root)
- {
- if(root==NULL)
- return root;
- else
- {
- invertTree(root->left);
- invertTree(root->right);
- struct TreeNode* mp;
- mp=root->left;
- root->left=root->right;
- root->right=mp;
- }
- return root;
- }
OJ题链接https://leetcode.cn/problems/symmetric-tree/
思路:
代码:
- bool _isSymmetric(struct TreeNode* L,struct TreeNode* R)
- {
- if(L->left==NULL && R->right==NULL)
- return true;
- if(L->left==NULL || R->right==NULL)
- return false;
- if(L->val!=R->val)
- return false;
- return _isSymmetric(L->left,R->right) && _isSymmetric(L->right,R->left);
- }
- bool isSymmetric(struct TreeNode* root)
- {
- if(root==NULL)
- {
- return true;
- }
- return _isSymmetric(root->left,root->right);
- }
思路:
看注释理解即可
- #include <stdio.h>
- #include <stdlib.h>
-
- struct TreeNode
- {
- char val;
- struct TreeNode* left;
- struct TreeNode* right;
- };
-
- //创建二叉树
- struct TreeNode* rebulidTree(char* str, int* i)
- {
- //= ‘#’直接返回,并将i++
- if(str[*i]=='#')
- {
- (*i)++;
- return NULL;
- }
- //不为‘#’首先malloc一个新的节点,并让root->val指向数组中对应下标位置的值
- struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
- root->val = str[(*i)++];
-
- //递归左子树和右子树
- root->left = rebulidTree(str, i);
- root->right = rebulidTree(str, i);
-
- return root;
- }
-
- //中序遍历
- void InOrder(struct TreeNode* root)
- {
- if(root==NULL)
- return;
- InOrder(root->left);
- printf("%c ",root->val);
- InOrder(root->right);
-
- }
-
- int main()
- {
- char str[100];
- scanf("%s",str);
-
- int i=0;
- //这里传参一定一定要传地址,因为在递归过程中虽然i++过了,
- //但只是在局部的栈帧中,返回之后栈帧销毁,i还是不会变的
- struct TreeNode* root = rebulidTree(str,&i);
-
- InOrder(root);
-
- return 0;
- }
思路:
如果不是完全二叉树,在出队列,出到NULL时,队列后面的元素会有不为空的情况
我们可以加入一个flag做个判断标志,在第一次遇到NULL是,将flag变为false
在只会再遇到不为空的元素时,如果flag为false,直接返回false
代码如下:
- //判断二叉树是否为完全二叉树:
- bool isCompleteTree(BTNode* root)
- {
- BTNode* queue[1000]; //直接用树的结构来生成一个队列(只用到data变量,不用到左右孩子)
-
- int head = 0, tail = 0; //head即队列的头节点,用来出数据,tail即队列的尾结点,用来入数据
-
- bool flag = false; // 存储是否出现过空节点的标记
-
- queue[tail++] = root; //先让队列的头=树的根节点
-
- while (head < tail)
- {
- BTNode* curNode = queue[head++];
-
- //在第一次遇到NULL的时候,用flag进行记录
- if (curNode == NULL)
- flag = true;
- else
- {
- if (flag) // 如果之前出现过空节点,说明当前节点不是完全二叉树的节点
- return false;
- queue[tail++] = curNode->left;
- queue[tail++] = curNode->right;
- }
- }
- return true;
- }
思路:递归进行销毁,和后序遍历类似
- void destroyTree(BTNode* root)
- {
- if (root == NULL)
- return;
- destroyTree(root->left);
- destroyTree(root->right);
- free(root);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。