Path [Algorithm] for a binary value and

Source title

Cattle off net offer to prove safety

Topic Introduction

Input nodes and a binary tree with an integer, the binary print values ​​of the nodes and the paths to all the input integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
    }
}

Topic ideas

The method of this tree structure, as long as not a simple traversal (because traversing the tree also allows you to use recursion, such subject simply get points, so test out the possibility is almost zero), basically think of recursion.
The thinking in the right direction would be directly analyzed in detail:
1. First, determine the recursive function, first the original function can not be used to traverse because of the function in line with requirements of the subject requires at least four parameters (where you can not use static variables). The first two requirements have been the subject variable are given, and is TreeNode * int type, there is a variable path, records the current path variable vector from the vertex to the vertex of the path, there is a variable that we return the result value of the variable vector <vector> ret;
to sum up
We need four variables as follows:

TreeNode* root;
int expectNumber;
vector<int> path;
vector<vector<int>>ret;

The above analysis of the recursive function may be constructed as follows:

void GetPath(TreeNode* p, int ex, vector<vector<int>>&resutl, vector<int>path)

First, determine the end of the recursive determination directly on the code:

//说明到达了叶节点了,并没有符合条件,路径和等于ex
else if (p == NULL)return;

Next, write a recursive logical parts:

//判断叶节点,如果符合路径和等于ex,则添加路径
if ((p->val == ex)&&(p->left==NULL)&&(p->right==NULL))
		{
			path.push_back(p->val);
			resutl.push_back(path);
		}
		else
		{
		//否则递归左子树和右子树。
			path.push_back(p->val);
			GetPath(p->left, ex - p->val, resutl, path);
			GetPath(p->right, ex - p->val, resutl, path);
		}

Source overall as follows

vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
		vector<vector<int>> ret;
		vector<int>path;
		if (root == NULL)return ret;
		path.push_back(root->val);
		//用于判断只有一个根节点的情况。
        if ((root->left == NULL) && (root->right == NULL))
		{
			ret.push_back(path);
			return ret;
		}
		expectNumber -= root->val;
		GetPath(root->left, expectNumber, ret, path);
		GetPath(root->right, expectNumber, ret, path);
		return ret;
	}

Overall code is as follows:

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
		vector<vector<int>> ret;
		vector<int>path;
		if (root == NULL)return ret;
		path.push_back(root->val);
        if ((root->left == NULL) && (root->right == NULL))
		{
			ret.push_back(path);
			return ret;
		}
		expectNumber -= root->val;
		GetPath(root->left, expectNumber, ret, path);
		GetPath(root->right, expectNumber, ret, path);
		return ret;
	}
	void GetPath(TreeNode* p, int ex, vector<vector<int>>&resutl, vector<int>path)
	{
        if (p == NULL)return;
			if ((p->val == ex) && (p->left == NULL) && (p->right == NULL))
		{
			path.push_back(p->val);
			resutl.push_back(path);
		}
		else
		{
			path.push_back(p->val);
			GetPath(p->left, ex - p->val, resutl, path);
			GetPath(p->right, ex - p->val, resutl, path);
		}
	}
};

ExplanationThe code is not optimized, here only to provide a problem-solving ideas, and I posted the source code to ensure that over the next premise.

Guess you like

Origin blog.csdn.net/u014128662/article/details/89061260