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
};
- package com.lhp;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
- class BinaryTree {
- private BinaryTreeNode root;
-
- public BinaryTreeNode getRoot() {
- return root;
- }
-
- public void setRoot(BinaryTreeNode root) {
- this.root = root;
- }
-
-
- public synchronized void addNode(BinaryTreeNode node) {
- if (null == this.root) {
- this.root = node;
- return;
- }
-
- BinaryTreeNode tempNode = this.root;
-
- while (true) {
- if (node.getM_nValue() > tempNode.getM_nValue()) {
- if (null == tempNode.getM_pRight()) {
- tempNode.setM_pRight(node);
- return;
- } else {
- tempNode = tempNode.getM_pRight();
- continue;
- }
- } else if (node.getM_nValue() < tempNode.getM_nValue()) {
- if (null == tempNode.getM_pLeft()) {
- tempNode.setM_pLeft(node);
- return;
- } else {
- tempNode = tempNode.getM_pLeft();
- continue;
- }
- } else {
- return;
- }
- }
- }
-
-
- public synchronized void printSumPath(int sumValue) {
- printSumPath(this.root, new ArrayList<Integer>(), 0, sumValue);
- }
-
-
- private void printSumPath(BinaryTreeNode node, List<Integer> path, int tempSum, int sumValue) {
- if (null == node) {
- return;
- }
-
- tempSum += node.getM_nValue();
- path.add(node.getM_nValue());
-
- boolean isLeaf = (null == node.getM_pLeft() && null == node.getM_pRight());
-
- if (isLeaf && tempSum == sumValue) {
- System.out.print("sumPath(" + sumValue + "): ");
- for (int i : path) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
-
-
- printSumPath(node.getM_pLeft(), path, tempSum, sumValue);
- printSumPath(node.getM_pRight(), path, tempSum, sumValue);
-
-
- path.remove(path.size() - 1);
-
- }
-
-
- public synchronized void print() {
- if (null == this.root) {
- System.out.print("HashCode: " + this.hashCode() + "; 空树;");
- return;
- }
- System.out.print("HashCode: " + this.hashCode() + "; 树: ");
- print(this.root);
- System.out.println();
- }
-
- private void print(BinaryTreeNode node) {
- if (null != node) {
- System.out.print(node.getM_nValue() + " ");
- print(node.getM_pLeft());
- print(node.getM_pRight());
- }
- }
- }
-
- class BinaryTreeNode {
- private int m_nValue;
- private BinaryTreeNode m_pLeft;
- private BinaryTreeNode m_pRight;
-
- BinaryTreeNode(int value) {
- this.m_nValue = value;
- }
-
- public int getM_nValue() {
- return m_nValue;
- }
- public void setM_nValue(int mNValue) {
- m_nValue = mNValue;
- }
- public BinaryTreeNode getM_pLeft() {
- return m_pLeft;
- }
- public void setM_pLeft(BinaryTreeNode mPLeft) {
- m_pLeft = mPLeft;
- }
- public BinaryTreeNode getM_pRight() {
- return m_pRight;
- }
- public void setM_pRight(BinaryTreeNode mPRight) {
- m_pRight = mPRight;
- }
- }
-
- public class Four {
- public static void main(String[] args) {
- BinaryTree tree = new BinaryTree();
- tree.addNode(new BinaryTreeNode(10));
- tree.addNode(new BinaryTreeNode(5));
- tree.addNode(new BinaryTreeNode(12));
- tree.addNode(new BinaryTreeNode(4));
- tree.addNode(new BinaryTreeNode(7));
- tree.addNode(new BinaryTreeNode(9));
- tree.addNode(new BinaryTreeNode(3));
- tree.print();
- tree.printSumPath(22);
- tree.printSumPath(31);
- }
- }