(转)在二元树中找出和为某一值的所有路径,java版本
摘自:http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如输入整数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;
- /**
- 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
- };
- */
- /**
- * 二叉树
- */
- class BinaryTree {
- private BinaryTreeNode root; // 根
- public BinaryTreeNode getRoot() {
- return root;
- }
- public void setRoot(BinaryTreeNode root) {
- this.root = root;
- }
- /**
- * 增加子节点
- * @param 节点
- */
- 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;
- }
- }
- }
- /**
- * 输出指定路径和大小的所有路径
- * @param 路径的和
- */
- public synchronized void printSumPath(int sumValue) {
- printSumPath(this.root, new ArrayList<Integer>(), 0, sumValue);
- }
- /**
- * @param 节点
- * @param 路径存储集合
- * @param 临时路径的和
- * @param 路径的和
- */
- 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); // 删除当前节点
- // 最后补充一下,如果path不是指针而是基本类型的话,这句话就没用了(放在递归调用下面就没用了),算法也废了,比如在这里加入一句tempSum+=XXX;对结果没有任何影响,不会影响递归return时其他函数里的参数
- }
- /**
- * 打印前序遍历
- */
- 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; // value of node
- private BinaryTreeNode m_pLeft; // left child of node
- private BinaryTreeNode m_pRight; // right child of node
- 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);
- }
- }
(转)在二元树中找出和为某一值的所有路径,java版本的更多相关文章
- 4.在二元树中找出和为某一值的所有路径[FindPathsInBinaryTree]
[题目]: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二元树 10 ...
- IT公司100题-4-在二元树中找出和为某一值的所有路径
问题描述: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数30和如下二元树 14 / \ 5 16 / ...
- python3实现在二叉树中找出和为某一值的所有路径
在二叉树中找出和为某一值的所有路径请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径.规则如下:1.从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设 ...
- 最短路径(给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。)
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 例: 输入: [ [1,3,1], [1,5,1], [ ...
- 找出所有从根节点到叶子节点路径和等于n的路径并输出
//找出所有从根节点到叶子节点路径和等于n的路径并输出 Stack<Node> stack = new Stack<Node>(); public void findPath( ...
- Excel VBA 找出选定范围不重复值和重复值
Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...
- 从键盘读入学生成绩,找出最高分, 并输出学生成绩等级(Java)
从键盘读入学生成绩,找出最高分, 并输出学生成绩等级 一.题目 从键盘读入学生成绩,找出最高分,并输出学生成绩等级. 成绩>=最高分-10 等级为'A' 成绩>=最高分-20 等级为'B' ...
- 如何找出当前活动桌面背景图像的位置/路径(Ubuntu 18.04,GNOME)?
启动终端并运行以下命令 $ gsettings get org.gnome.desktop.background picture-uri 显示当前设置为桌面背景图片的完整路径.
- 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
给定两个字符串 s 和 t,它们只包含小写字母.字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母.请找出在 t 中被添加的字母. 示例: 输入: s = "abcd" ...
随机推荐
- codeforces 357
C 题意: ###n个勇士编号1-n,m个回合对战,每个回合由仍留在游戏里的编号Li~Ri的人参加,胜者为Xi,输的人退出游戏. ###求一个a1-an的序列,若ai为胜者,则ai=0,否则ai=打败 ...
- 智能穿戴设备移动APP端与外设数据传输协议功能模块CMD&ACK表
Notification Module Function CMD ACK Notification History Count [0x0301] [0x0000] [0x01] [0x0301] [0 ...
- lucene学习-2 - 一个示例
接下来我会写一个lucene的实例.实际上在搜索引擎上随便搜索下都能找到这样的东西.不过还是写一下吧,这也是我学习的经历. package com.zhyea.doggie; import java. ...
- JNI_Z_09_Java的字符串
ZC: jstring 就是 Java中的String对象 ZC: 10.8 Unicode字符串结尾(http://www.360doc.cn/article/14233282_321497569. ...
- c#实现验证某个IP地址是否能ping通
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- python模块及模块安装
其实python的模块及模块安装和其他编程语言,如:nodeJs.reactJs的相同,只不过他们使用包管理工具不相同而已,python用pip,而node用npm python 模块 python语 ...
- 条款10:让operator=返回一个reference to *this
例如对象x,y,z.要实现连锁赋值(假设operator=已经重载过了):x = y = z,那么operator=则必须返回一个*this. 注意这个条款不仅仅适合于operator=,对于oper ...
- 《模式 工程化实现及扩展 (设计模式 C#版)》 - 书摘精要
(P3) 面向对象的典型原则可以划分为两类 —— “面向类”的和“面向包”的: “面向类”的,包括:SRP —— 单一职责原则:OCP —— 开放封闭原则:LSP —— 里氏替换原则:DIP —— 依 ...
- Electron 使用 Webpack2 预编译 Electron 和 Browser targets
Electron 使用 Webpack2 预编译 Electron 和 Browser targets 前一篇文章说了说怎样使用 Webpack2 预编译 Electron 应用,但是有时候我们希望使 ...
- Unity3d command line arguments
Options Many of these relate to Unity3d command line arguments Batch Mode - should be left enabled u ...