【剑指】34.二叉树中和为某一值的路径

题目描述

  • 输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

算法分析

  • 递归先序遍历树, 把结点加入路径;若该结点是叶子结点,则比较当前路径和是否等于期待和;
  • 每次递归返回时,当前路径也应该回退一个结点;
  • 加入了对当前路径和的判断,而不是遍历到叶子节点才判断路径和,减少了递归调用;

提交代码:

class Solution {
public:
	vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
		
		if (!root || expectNumber <= 0)
			return vector<vector<int> >();

		vector<vector<int> > result;
		vector<int> path;

		FindPathCore(result, path, root, 0, expectNumber);

		return result;
	}


	void FindPathCore(vector<vector<int> > &result, vector<int> &path, 
		TreeNode* node, int sum, int expectNumber)
	{
		int curSum = sum + node->val;
		// 减少递归调用次数
		if (curSum > expectNumber)
			return;

		path.push_back(node->val);

		if (node->left)
			FindPathCore(result, path, node->left, curSum, expectNumber);
		if (node->right)
			FindPathCore(result, path, node->right, curSum, expectNumber);
		if (!node->left && !node->right)
		{
			if (curSum == expectNumber)
				result.push_back(path);
		}

		path.pop_back();
	}
};

测试代码:

// ====================测试代码====================
void Test(char* testName, TreeNode* pRoot, int expectedSum)
{
	if (testName != nullptr)
		printf("%s begins:\n", testName);

	Solution s;
	vector<vector<int> > result = s.FindPath(pRoot, expectedSum);
	for (vector<int> temp : result)
	{
		for (int each : temp)
			cout << each << " ";
		cout << endl;
	}
}

//            10
//         /      \
//        5        12
//       /\        
//      4  7     
// 有两条路径上的结点和为22
void Test1()
{
	TreeNode* pNode10 = CreateBinaryTreeNode(10);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode12 = CreateBinaryTreeNode(12);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode7 = CreateBinaryTreeNode(7);

	ConnectTreeNodes(pNode10, pNode5, pNode12);
	ConnectTreeNodes(pNode5, pNode4, pNode7);

	printf("Two paths should be found in Test1.\n");
	Test("Test1", pNode10, 22);

	DestroyTree(pNode10);
}

//            10
//         /      \
//        5        12
//       /\        
//      4  7     
// 没有路径上的结点和为15
void Test2()
{
	TreeNode* pNode10 = CreateBinaryTreeNode(10);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode12 = CreateBinaryTreeNode(12);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode7 = CreateBinaryTreeNode(7);

	ConnectTreeNodes(pNode10, pNode5, pNode12);
	ConnectTreeNodes(pNode5, pNode4, pNode7);

	printf("No paths should be found in Test2.\n");
	Test("Test2", pNode10, 15);

	DestroyTree(pNode10);
}

//               5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
// 有一条路径上面的结点和为15
void Test3()
{
	TreeNode* pNode5 = CreateBinaryTreeNode(5);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode3 = CreateBinaryTreeNode(3);
	TreeNode* pNode2 = CreateBinaryTreeNode(2);
	TreeNode* pNode1 = CreateBinaryTreeNode(1);

	ConnectTreeNodes(pNode5, pNode4, nullptr);
	ConnectTreeNodes(pNode4, pNode3, nullptr);
	ConnectTreeNodes(pNode3, pNode2, nullptr);
	ConnectTreeNodes(pNode2, pNode1, nullptr);

	printf("One path should be found in Test3.\n");
	Test("Test3", pNode5, 15);

	DestroyTree(pNode5);
}

// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
// 没有路径上面的结点和为16
void Test4()
{
	TreeNode* pNode1 = CreateBinaryTreeNode(1);
	TreeNode* pNode2 = CreateBinaryTreeNode(2);
	TreeNode* pNode3 = CreateBinaryTreeNode(3);
	TreeNode* pNode4 = CreateBinaryTreeNode(4);
	TreeNode* pNode5 = CreateBinaryTreeNode(5);

	ConnectTreeNodes(pNode1, nullptr, pNode2);
	ConnectTreeNodes(pNode2, nullptr, pNode3);
	ConnectTreeNodes(pNode3, nullptr, pNode4);
	ConnectTreeNodes(pNode4, nullptr, pNode5);

	printf("No paths should be found in Test4.\n");
	Test("Test4", pNode1, 16);

	DestroyTree(pNode1);
}

// 树中只有1个结点
void Test5()
{
	TreeNode* pNode1 = CreateBinaryTreeNode(1);

	printf("One path should be found in Test5.\n");
	Test("Test5", pNode1, 1);

	DestroyTree(pNode1);
}

// 树中没有结点
void Test6()
{
	printf("No paths should be found in Test6.\n");
	Test("Test6", nullptr, 0);
}

int main(int argc, char* argv[])
{
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();
	Test6();

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ansizhong9191/article/details/80952283
今日推荐