面试题:在二元树中找出和为某一值的所有路径

在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树。

从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。

打印出和与输入整数相等的所有路径。 例如输入整数 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 
};

Java参考

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EthanMilk

你的鼓励是我创作的最大动力谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值