java之二叉树和为某一值的路径
来源:互联网 发布:苹果笔记本实用软件 编辑:程序博客网 时间:2024/05/22 00:09
题目:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径,从树的根结点开始往下一直到叶子结点所经过的结点形成一条路径。二叉树结点定义如下:
class BiTree {int value;BiTree lchild, rchild;public BiTree(int value) {this.value = value;lchild = null;rchild = null;}public int getValue() {return value;}public void setValue(int value) {this.value = value;}public BiTree getLchild() {return lchild;}public void setLchild(BiTree lchild) {this.lchild = lchild;}public BiTree getRchild() {return rchild;}public void setRchild(BiTree rchild) {this.rchild = rchild;}}
分析:用前序遍历的方式访问到某一结点时,我们把该节点添加到路径上,并累计该结点的值。如果该节点是叶子节点并且路径中的值的和刚好等于输入的整数,则当前路径符合要求,我们把它打印出来。如果当前结点不是叶子结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到它的父结点。因此我们函数退出之前要在路径上删除当前结点并减去当前结点的值,一确保返回父节点时的路径刚好是从根结点到父结点的路径。实际上是栈,因为路径要与递归调用状态一致。
下面为完整代码:
package Tree;import java.util.Iterator;import java.util.Stack;public class FindPathMian {public static void findpath(BiTree root, int expectedSum) {if (root == null)return;Stack<Integer> path = new Stack<Integer>();int currentSum = 0;findpath(root, expectedSum, path, currentSum);}public static void findpath(BiTree root, int expectedSum,Stack<Integer> path, int currentSum) {currentSum += root.value;path.push(root.value);boolean isLeaf = root.lchild == null && root.rchild == null;if (currentSum == expectedSum && isLeaf) {System.out.println("A path is found:");Iterator<Integer> it = path.iterator();while (it.hasNext()) {System.out.print(it.next() + " ");}}if (root.lchild != null)findpath(root.lchild, expectedSum, path, currentSum);if (root.rchild != null)findpath(root.rchild, expectedSum, path, currentSum);path.pop();}public static BiTree ConstructCore(int[] preOder, int[] inOder,int startpreOder, int startinOder, int endpreOder, int endinOder) {int rootValue = preOder[startpreOder];BiTree root = new BiTree(rootValue);// 在中序遍历中找到根节点int mid = startinOder;while (mid <= endinOder && inOder[mid] != rootValue)++mid;int leftLength = mid - startinOder;int leftPreOderEnd = startpreOder + leftLength;if (leftLength > 0) {// 构建左子树root.setLchild(ConstructCore(preOder, inOder, startpreOder + 1,startinOder, leftPreOderEnd, mid - 1));}if (leftLength < endpreOder - startpreOder) {// 构建右子树root.setRchild(ConstructCore(preOder, inOder, leftPreOderEnd + 1,mid + 1, endpreOder, endinOder));}return root;}public static void main(String[] args) {int preOder[] = { 1, 2, 4, 7, 3, 5, 6, 8 };int inOder[] = { 4, 7, 2, 1, 5, 3, 8, 6 };BiTree trr = ConstructCore(preOder, inOder, 0, 0, 7, 7);findpath(trr, 14);}}
结果为:
A path is found:
1,2,4,7
0 0
- java之二叉树和为某一值的路径
- 牛客网刷题之二叉树中和为某一值的路径
- 二叉树中和为某一值的路径 java实现
- Java分析:二叉树中和为某一值的路径
- 华为机试题之在二叉树中找出和为某一值的所有路径(java语言)
- 二叉树路径的查找(和为某一值)
- 二叉树打印和为某一值的路径
- 打印二叉树和为某一值的路径
- 二叉树和为某一值的路径
- 二叉树和为某一值的路径
- 二叉树中的和为某一值的路径
- C++算法之 在二叉树当中找出和为某一值的路径
- 程序员面试金典--面试24之二叉树和为某一值的路径
- 在二叉树中找出和为某一值的所有路径-java实现
- 剑指offer-二叉树中和为某一路径的和
- 【学习点滴-数据结构-二叉树】和为某一值的二叉树路径~
- 【二叉树3】判断二叉树和为某一值的路径
- 打印二叉树中路径和为某一值的所有路径
- 如何学STM32—十年经验教你如何学习嵌入式系统
- ndk-r9d下ffmpeg-2.6.3的编译
- continue 循环直接跳到外层
- C语言----多维数组
- root帐号在home目录下建的用户怎么删掉?
- java之二叉树和为某一值的路径
- java.lang.OutOfMemoryError: Java heap space解决方法
- 黑马程序员---关键字、常量、变量、运算符
- Android5.0最新特性一览,看看有没有你需要的
- 让BeagleBone上的USR0闪烁起来
- 第二章,自定义函数(Lua)
- IO学习笔记
- 分析一下github中项目的软件启动画面的实现
- Shell命令:echo介绍,echo如何输出带颜色的文本