Giter Club home page Giter Club logo

Comments (3)

zdszero avatar zdszero commented on August 13, 2024 1

收到,谢谢你的提示,题解 已修改

from ustc-cs-graduate.

schrieffer-z avatar schrieffer-z commented on August 13, 2024
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;

	TreeNode() {};
	TreeNode(int x) :val(x) {};
	TreeNode(int x, TreeNode* l, TreeNode* r) :val(x), left(l),right(r) {};
};

struct info {
	int low;
	int up;
	info(int a, int b) : low(a), up(b) {};
};


int gb_ret;
int maxChainLen(TreeNode* root) {
	if (!root) return 0;

	int llen = maxChainLen(root->left);
	int rlen = maxChainLen(root->right);

	//若去掉这行关于全局变量gb_ret的代码,那maxChainLen就是求从某个结点向下的最长路径
	//若保留,则:
	//每次递归走到此处时,我们已经知道了从左、右走的两条最长路,那么我们也就知道了在该点“拐弯”的最长路径
	//又由于maxChainLen会遍历所以结点,那我们相应地也就穷举了在所有点“拐弯”的路径
	gb_ret = fmax(root->val + llen + rlen, gb_ret);

	return fmax(llen, rlen) + root->val;
}


void t5() {
	int k;
	ifstream fin("tree.in");
	TreeNode* root;

	queue<pair<TreeNode**, info*>> q;
	pair<TreeNode**, info*> rootpair = { &root, new info(-1,INT32_MAX) };
	q.push(rootpair);
	while (fin >> k) {
		TreeNode** T;
		info* ifm;
		while (true) {
			T = q.front().first;
			ifm = q.front().second;
			q.pop();

			if (ifm->low < k && k < ifm->up) {
				break;
			}
		}
		
		*T = new TreeNode(k, NULL, NULL);
		//注意->优先于&
		pair<TreeNode**, info*> leftpair = { &(*T)->left, new info(ifm->low, k) };
		pair<TreeNode**, info*> rightpair = { &(*T)->right, new info(k,ifm->up) };

		q.push(leftpair);
		q.push(rightpair);
	}

	gb_ret = -1;
	maxChainLen(root);
	cout << gb_ret;
}

from ustc-cs-graduate.

schrieffer-z avatar schrieffer-z commented on August 13, 2024

根据本图:
image
那么示例也应该改为:

输入示例:
25 15 40 5 20 30 50 10 35
输出示例:
165

from ustc-cs-graduate.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.