在二元树中找出和为某一值的所有路径-递归算法
2009-11-20 10:03
204 查看
题目:输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。
例如输入整数22和如下二元树
10
/ /
5 12
/ /
4 7
则打印出两条路径:10, 12和10, 5, 7。
解题思路:
(1)
考查对树这种基本数据结构以及递归函数的理解。
当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根结点到父结点的路径。我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。
代码如下:
class Tree {
Tree left, right;
int item;
public Tree(Tree left, Tree right, int item) {
this.left = left;
this.right = right;
this.item = item;
}
}
// /////////////////////////////////////////////////////////////////////
// Find paths whose sum equal to expected sum
// 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。
// /////////////////////////////////////////////////////////////////////
void findPath(Tree root, int expectsum, Vector path, int currentsum) {
if (root == null) {
return;
}
currentsum += root.item;
path.add(root.item);
boolean isLeaf = ((root.left == null) && (root.right == null));
if (currentsum == expectsum && isLeaf) {
for (int i = 0; i < path.size(); i++) {
System.out.print(path.get(i) + " ");
}
System.out.println();
}
// if the node is not a leaf, goto its children
if (root.left != null)
findPath(root.left, expectsum, path, currentsum);
if (root.right != null)
findPath(root.right, expectsum, path, currentsum);
// when we finish visiting a node and return to its parent node,
// we should delete this node from the path and
// minus the node's value from the current sum
currentsum -= root.item;
path.removeElement(root.item);
}
例如输入整数22和如下二元树
10
/ /
5 12
/ /
4 7
则打印出两条路径:10, 12和10, 5, 7。
解题思路:
(1)
考查对树这种基本数据结构以及递归函数的理解。
当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根结点到父结点的路径。我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。
代码如下:
class Tree {
Tree left, right;
int item;
public Tree(Tree left, Tree right, int item) {
this.left = left;
this.right = right;
this.item = item;
}
}
// /////////////////////////////////////////////////////////////////////
// Find paths whose sum equal to expected sum
// 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。
// /////////////////////////////////////////////////////////////////////
void findPath(Tree root, int expectsum, Vector path, int currentsum) {
if (root == null) {
return;
}
currentsum += root.item;
path.add(root.item);
boolean isLeaf = ((root.left == null) && (root.right == null));
if (currentsum == expectsum && isLeaf) {
for (int i = 0; i < path.size(); i++) {
System.out.print(path.get(i) + " ");
}
System.out.println();
}
// if the node is not a leaf, goto its children
if (root.left != null)
findPath(root.left, expectsum, path, currentsum);
if (root.right != null)
findPath(root.right, expectsum, path, currentsum);
// when we finish visiting a node and return to its parent node,
// we should delete this node from the path and
// minus the node's value from the current sum
currentsum -= root.item;
path.removeElement(root.item);
}
相关文章推荐
- 程序员面试题精选(4):在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 4.在二元树中找出和为某一值的所有路径
- 程序员面试100题---4.在二元树中找出和为某一值的所有路径(树)
- 每天一算法(在二元树中找出和为某一值的所有路径(树))
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径
- 每天一道算法题(9)——在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 【面试题】在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 4. 微软面试题: 在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- flag2在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数
- 在二元树中找出和为某一值的所有路径
- 4.在二元树中找出和为某一值的所有路径(树)
- 算法习题5:在二元树中找出和为某一值的所有路径
- NO_4在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径