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

摘自: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 
}; 

Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.lhp;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. /** 
  10.     4.在二元树中找出和为某一值的所有路径 
  11.     题目:输入一个整数和一棵二元树。 
  12.     从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 
  13.     打印出和与输入整数相等的所有路径。 
  14.     例如输入整数22 和如下二元树 
  15.     10 
  16.     / \ 
  17.     5 12 
  18.     /\ 
  19.     4 7 
  20.     则打印出两条路径:10, 12 和10, 5, 7。 
  21.     二元树节点的数据结构定义为: 
  22.     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. }  

posted on 2015-10-14 09:44  antyi  阅读(222)  评论(0编辑  收藏  举报

导航