摘自:http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html

4.在二元树中找出和为某一值的所有路径 
题目:输入一个整数和一棵二元树。 
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 
打印出和与输入整数相等的所有路径。 
例如输入整数22 和如下二元树 
10 
/ \ 
5 12 
/\ 
4 7 
则打印出两条路径:10, 12 和10, 5, 7。 
二元树节点的数据结构定义为: 
struct BinaryTreeNode // a node in the binary tree 

int m_nValue; // value of node 
BinaryTreeNode *m_pLeft; // left child of node 
BinaryTreeNode *m_pRight; // right child of node 
};

  1. /**
  2. *
  3. */
  4. package com.lhp;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. 4.在二元树中找出和为某一值的所有路径
  9. 题目:输入一个整数和一棵二元树。
  10. 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
  11. 打印出和与输入整数相等的所有路径。
  12. 例如输入整数22 和如下二元树
  13. 10
  14. / \
  15. 5 12
  16. /\
  17. 4 7
  18. 则打印出两条路径:10, 12 和10, 5, 7。
  19. 二元树节点的数据结构定义为:
  20. struct BinaryTreeNode // a node in the binary tree
  21. {
  22. int m_nValue; // value of node
  23. BinaryTreeNode *m_pLeft; // left child of node
  24. BinaryTreeNode *m_pRight; // right child of node
  25. };
  26. */
  27. /**
  28. * 二叉树
  29. */
  30. class BinaryTree {
  31. private BinaryTreeNode root;    // 根
  32. public BinaryTreeNode getRoot() {
  33. return root;
  34. }
  35. public void setRoot(BinaryTreeNode root) {
  36. this.root = root;
  37. }
  38. /**
  39. * 增加子节点
  40. * @param 节点
  41. */
  42. public synchronized void addNode(BinaryTreeNode node) {
  43. if (null == this.root) {
  44. this.root = node;
  45. return;
  46. }
  47. BinaryTreeNode tempNode = this.root;
  48. while (true) {
  49. if (node.getM_nValue() > tempNode.getM_nValue()) {   // 大于父节点
  50. if (null == tempNode.getM_pRight()) {
  51. tempNode.setM_pRight(node);
  52. return;
  53. } else {
  54. tempNode = tempNode.getM_pRight();
  55. continue;
  56. }
  57. } else if (node.getM_nValue() < tempNode.getM_nValue()) {    // 小于父节点
  58. if (null == tempNode.getM_pLeft()) {
  59. tempNode.setM_pLeft(node);
  60. return;
  61. } else {
  62. tempNode = tempNode.getM_pLeft();
  63. continue;
  64. }
  65. } else {    // 等于父节点
  66. return;
  67. }
  68. }
  69. }
  70. /**
  71. * 输出指定路径和大小的所有路径
  72. * @param 路径的和
  73. */
  74. public synchronized void printSumPath(int sumValue) {
  75. printSumPath(this.root, new ArrayList<Integer>(), 0, sumValue);
  76. }
  77. /**
  78. * @param 节点
  79. * @param 路径存储集合
  80. * @param 临时路径的和
  81. * @param 路径的和
  82. */
  83. private void printSumPath(BinaryTreeNode node, List<Integer> path, int tempSum, int sumValue) {
  84. if (null == node) {
  85. return;
  86. }
  87. tempSum += node.getM_nValue();
  88. path.add(node.getM_nValue());
  89. boolean isLeaf = (null == node.getM_pLeft() && null == node.getM_pRight()); // 是否为叶子
  90. if (isLeaf && tempSum == sumValue) {    // 满足
  91. System.out.print("sumPath(" + sumValue + "): ");
  92. for (int i : path) {
  93. System.out.print(i + " ");
  94. }
  95. System.out.println();
  96. }
  97. // 《向左走,向右走》 :-)
  98. printSumPath(node.getM_pLeft(), path, tempSum, sumValue);
  99. printSumPath(node.getM_pRight(), path, tempSum, sumValue);
  100. // 保证递归完成后返回父节点时路径是根结点到父节点的路径,之后遍历父节点的其他子节点,没有则返回到爷爷节点...
  101. path.remove(path.size() - 1);   // 删除当前节点
  102. // 最后补充一下,如果path不是指针而是基本类型的话,这句话就没用了(放在递归调用下面就没用了),算法也废了,比如在这里加入一句tempSum+=XXX;对结果没有任何影响,不会影响递归return时其他函数里的参数
  103. }
  104. /**
  105. * 打印前序遍历
  106. */
  107. public synchronized void print() {
  108. if (null == this.root) {
  109. System.out.print("HashCode: " + this.hashCode() +  "; 空树;");
  110. return;
  111. }
  112. System.out.print("HashCode: " + this.hashCode() +  "; 树: ");
  113. print(this.root);
  114. System.out.println();
  115. }
  116. private void print(BinaryTreeNode node) {
  117. if (null != node) {
  118. System.out.print(node.getM_nValue() + " ");
  119. print(node.getM_pLeft());
  120. print(node.getM_pRight());
  121. }
  122. }
  123. }
  124. /**
  125. * 节点
  126. */
  127. class BinaryTreeNode {
  128. private int m_nValue; // value of node
  129. private BinaryTreeNode m_pLeft; // left child of node
  130. private BinaryTreeNode m_pRight; // right child of node
  131. BinaryTreeNode(int value) {
  132. this.m_nValue = value;
  133. }
  134. public int getM_nValue() {
  135. return m_nValue;
  136. }
  137. public void setM_nValue(int mNValue) {
  138. m_nValue = mNValue;
  139. }
  140. public BinaryTreeNode getM_pLeft() {
  141. return m_pLeft;
  142. }
  143. public void setM_pLeft(BinaryTreeNode mPLeft) {
  144. m_pLeft = mPLeft;
  145. }
  146. public BinaryTreeNode getM_pRight() {
  147. return m_pRight;
  148. }
  149. public void setM_pRight(BinaryTreeNode mPRight) {
  150. m_pRight = mPRight;
  151. }
  152. }
  153. public class Four {
  154. public static void main(String[] args) {
  155. BinaryTree tree = new BinaryTree();
  156. tree.addNode(new BinaryTreeNode(10));
  157. tree.addNode(new BinaryTreeNode(5));
  158. tree.addNode(new BinaryTreeNode(12));
  159. tree.addNode(new BinaryTreeNode(4));
  160. tree.addNode(new BinaryTreeNode(7));
  161. tree.addNode(new BinaryTreeNode(9));
  162. tree.addNode(new BinaryTreeNode(3));
  163. tree.print();
  164. tree.printSumPath(22);
  165. tree.printSumPath(31);
  166. }
  167. }

(转)在二元树中找出和为某一值的所有路径,java版本的更多相关文章

  1. 4.在二元树中找出和为某一值的所有路径[FindPathsInBinaryTree]

    [题目]: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二元树 10              ...

  2. IT公司100题-4-在二元树中找出和为某一值的所有路径

    问题描述: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数30和如下二元树   14 / \ 5 16 / ...

  3. python3实现在二叉树中找出和为某一值的所有路径

    在二叉树中找出和为某一值的所有路径请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径.规则如下:1.从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设 ...

  4. 最短路径(给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。)

    给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 例: 输入: [ [1,3,1], [1,5,1], [ ...

  5. 找出所有从根节点到叶子节点路径和等于n的路径并输出

    //找出所有从根节点到叶子节点路径和等于n的路径并输出 Stack<Node> stack = new Stack<Node>(); public void findPath( ...

  6. Excel VBA 找出选定范围不重复值和重复值

    Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...

  7. 从键盘读入学生成绩,找出最高分, 并输出学生成绩等级(Java)

    从键盘读入学生成绩,找出最高分, 并输出学生成绩等级 一.题目 从键盘读入学生成绩,找出最高分,并输出学生成绩等级. 成绩>=最高分-10 等级为'A' 成绩>=最高分-20 等级为'B' ...

  8. 如何找出当前活动桌面背景图像的位置/路径(Ubuntu 18.04,GNOME)?

    启动终端并运行以下命令 $ gsettings get org.gnome.desktop.background picture-uri 显示当前设置为桌面背景图片的完整路径.  

  9. 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。

    给定两个字符串 s 和 t,它们只包含小写字母.字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母.请找出在 t 中被添加的字母. 示例: 输入: s = "abcd" ...

随机推荐

  1. [CF19E]Fairy

    给定一张n个点m条边的无向图,求删除哪一条边后,能够确保构成一个二分图,输出所有可能 解法:我们知道二分图的性质是没有奇环,这道题我们也应该从这个方面入手来考虑. 如果没有奇环的话我们当然想怎么删就怎 ...

  2. Effective C++ 条款11:在operator=中处理"自我赋值"

    "自我赋值"发生在对象被赋值给自己时: class Widget { ... }; Widget w; ... w = w; // 赋值给自己 a[i] = a[j]; // 潜在 ...

  3. sql行列互换

    出现这个结果: sql如下: end) as erjidu from a GROUP BY y;

  4. struts2——多文件上传

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 用Java编程计算兔子生兔子的问题

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 程序分析: 这是一个典型的Fibonacci数列问 ...

  6. EditText实现输入限制和校验

    EditText实现输入限制和校验 一.方法 1)输入限制 1.通过android:digits限制只能输入小写abc android:digits="abc" 2.通过andro ...

  7. 一台电脑上同时使用两个github账户

    需求:公司有github账号,自己有github账号,想在Git上同时使用,两者互不干扰. 思路:管理两个SHH key. 解决办法: 一.生成两个SSH key 为了举例方便,这里使用“one”和“ ...

  8. Exception has been thrown by the target of an invocation 网站报错

    最近因为要做一个启动器,在使用WPF做UI的时候,发现有错误如下: 错误 1 未知的生成错误"此实现不是 Windows 平台 FIPS 验证的加密算法的一部分. 行 8 位置 3.&quo ...

  9. JsonTools 工具类

    import net.sf.json.JSONObject; public class JsonTools { public static JSONObject getJSONObject(Strin ...

  10. pycharm 设置 默认信息

    在pycharm使用过程中,对于每次新建文件的shebang行和关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择File-Settings, 2 ...