1-30、31总结

这两天有事,就AC了几道题

题目链接:新二叉树

题解:因为每个节点的数据都是小写字母a到z中的一个,所以我们可以直接用数组下标记录是什么字符,减少了遍历所有字符来寻找儿子的繁琐步骤定义一个结构体,两个指针分别指向左儿子和右儿子,遍历输出即可

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(h,n) for(int i=h;i<=n;i++)
#define down(h,n) for(int i=h;i>=n;i--)
#define wh(x) while(x--)
#define node struct node
#define ios ios::sync_with_stdio(false)
constexpr int N = 305;
constexpr int mod = 1e9 + 7;
typedef int SElemType;


node{
	char  left;
    char  right;
}tree[28];
int n;
void dfs(char x) {
	if (x == '*') return;
	cout << x;
	dfs(tree[x - 97].left); //遍历左子树
	dfs(tree[x - 97].right);// 遍历右子树
	return;
}
int main()
{
	cin >> n;
	char a;
	cin >> a;
	cin >> tree[a-97].left >> tree[a-97].right;
	up(0, n - 2) {
		char s;
		cin >> s;
		cin >> tree[s - 97].left >> tree[s - 97].right;
	}
	dfs(a);
	return 0;

}

题目链接:求先序排列

题解:后序遍历的最后一个就是树的根节点,我们就在中序遍历中找出根节点的位置,根节点的左边就是左子树,右边就是右子树。截取左子树和右子树,重复上述操作,直到左子树和右子树都是叶子节点

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(h,n) for(int i=h;i<=n;i++)
#define down(h,n) for(int i=h;i>=n;i--)
#define wh(x) while(x--)
#define node struct node
#define ios ios::sync_with_stdio(false)
constexpr int N = 305;
constexpr int mod = 1e9 + 7;
typedef int SElemType;


string a;
string b;
void dfs(string s, string k) {
	if (s.size() > 0) {
		char ch = k[k.size() - 1];//找到后序的根结点
		cout << ch;
		int t = s.find(ch); // find用来查找位置
		dfs(s.substr(0, t), k.substr(0, t));// substr用来截取字符串
		dfs(s.substr(t + 1), k.substr(t, k.size()-t- 1));//递归左右两边子树
	}
	else return ;
}
int main()
{
	cin >> a;
	cin >> b;
	dfs(a, b);
	return 0;
}

题目链接:二叉树深度

题解:从根节点出发,先递归遍历该节点的左节点,再递归遍历该节点的右节点,其中还要记录该节点的深度,出发时深度为1。

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(i, h, n) for (int i = h; i <= n; i++) 
#define down(i, h, n) for(int i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int N = 1000005;
constexpr int mod = 1e9 + 7;

node{
	int left;
    int right;
};
node tree[N];
int n,sum;
void dfs(int s, int k) {
	if (s == 0) return;
	sum = max(sum, k);  //找最大深度
	dfs(tree[s].left, k + 1); //遍历左子树深度
	dfs(tree[s].right, k + 1);// 遍历右子树深度
}
int main() {
	Ios;
	cin >> n;
	up(i, 1, n) {
		cin >> tree[i].left >> tree[i].right;
	}
	dfs(1, 1);
	cout << sum;
	return 0;
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值