当前位置: 首页 > news >正文

二叉树中的和为某一值的路径

一,问题描述

给定一棵二叉树 和 一个整数,打印出二叉树中结点值的和为给定的整数的所有路径。注意:路径是指:从二叉树的根结点开始的,往下一直到叶子结点过程中 所经过的结点(包括根结点(起点)和叶子结点(终点))。

其中,关于二叉树相关知识可参考:二叉查找的递归实现及递归分析(http://www.cnblogs.com/hapjin/p/5390451.html)

 

二,算法分析

应用了递归的先序遍历。先序遍历每个结点,将先序遍历的每个结点的和相加,相加的结果是给定的整数,则找到一条路径并打印之。

定义一个整型变量currentSum,保存当前访问到的路径上的结点之和;定义一个栈用来保存当前的路径。为什么用栈?因为这样可以与递归的先序遍历同步。

用了栈之后,如何打印路径?JAVA的LinkedList类的 descendingIterator()方法返回一个可以逆序遍历链表的迭代器。

结点中的值,即可以为正数 也可以为负数吗?答案是可以。因为,路径必须是从根到叶子结点,且整个递归调用会一直调用到叶子后才会返回。

代码实现如下:

复制代码
 1 public void findExpectedSumPath(BinaryNode root, int expectedSum){
 2         if(root == null)
 3             return;
 4         LinkedList<BinaryNode> stack = new LinkedList<BinaryNode>();
 5         int currentSum = 0;
 6         findExpectedSumPath(root, expectedSum, currentSum, stack);
 7     }
 8     private void findExpectedSumPath(BinaryNode root, int expectedSum, int currentSum, LinkedList<BinaryNode> stack){
 9         currentSum += root.element;
10         stack.push(root); // visit root
11         
12         boolean isLeaf = (root.left == null && root.right == null);
13         if(currentSum == expectedSum && isLeaf){//print path
14             Iterator<BinaryNode> it = stack.descendingIterator();//逆序遍历List(Stack)
15             while(it.hasNext())
16                 System.out.print(it.next().element + " ");
17             
18             System.out.println();
19         }//end if
20         
21         if(root.left != null)//visit left sub tree
22             findExpectedSumPath(root.left, expectedSum, currentSum, stack);
23         if(root.right != null)//visit right sub tree
24             findExpectedSumPath(root.right, expectedSum, currentSum, stack);
25         
26         stack.pop();//当某结点左右孩子均为null时, 回退
27     }
复制代码

可以看出:上面的代码是一个二叉树的先序遍历的典型应用!第9,10行表示:访问根结点[然后进行了一系列的处理(12-19行)]第21、22行表示访问根的左子树第23、24行表示访问根的右子树。

当遍历到叶子结点时,在第26行,保存路径的栈会 pop,这相当于路径的回退。

 

三,扩展

如果给定的树中的结点值都是正数,且路径只需要从根结点开始,路径的终点不一定是叶子结点

这里,当currentSum > expectedSum时,就不需要再向下进行递归调用了。因为,结点值都是正数,再向下递归调用只会是currentSum越来越大。

比如,expectedSum为 13,当遍历到结点6时,currentSum=8+6=14 了,就不需要再向下进行遍历了。

如果expectedSum=14,那么遍历到6之后,也不需要再向下遍历了。总之,只有当currentSum 小于 expectedSum时,才需要向下进行遍历。代码实现如下:

复制代码
 1 public void findExpectedSumPath2(BinaryNode root, int expectedSum){
 2         if(root == null)
 3             return;
 4         LinkedList<BinaryNode> stack = new LinkedList<BinaryNode>();
 5         int currentSum = 0;
 6         findExpectedSumPath2(root, expectedSum, currentSum, stack);
 7     }
 8     private void findExpectedSumPath2(BinaryNode root, int expectedSum, int currentSum, LinkedList<BinaryNode> stack){
 9         currentSum += root.element;
10         stack.push(root);
11         
12 //        boolean isLeaf = (root.left == null && root.right == null);
13         if(currentSum == expectedSum){//print path
14             Iterator<BinaryNode> it = stack.descendingIterator();//逆序遍历List(Stack)
15             while(it.hasNext())
16                 System.out.print(it.next().element + " ");
17             
18             System.out.println();
19         }//end if
20         
21         //当currentSum > expectedSum时 再往下递归没有意义了
22         if(currentSum < expectedSum){
23             if(root.left != null)
24                 findExpectedSumPath2(root.left, expectedSum, currentSum, stack);
25             if(root.right != null)
26                 findExpectedSumPath2(root.right, expectedSum, currentSum, stack);
27         }
28         
29         stack.pop();//当某结点左右孩子均为null时, 回退
30     }
复制代码

 

完整代码如下:

复制代码
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class ExpectedSumPath {
    
    private class BinaryNode{
        int element;
        BinaryNode left;
        BinaryNode right;
        
        public BinaryNode(int ele) {
            element = ele;
            this.left = this.right = null;
        }
    }
    
    private BinaryNode root;//树根
    
    public void insert(int element){
        root = insert(element, root);
    }
    private BinaryNode insert(int element, BinaryNode root){
        if(root == null)
            return new BinaryNode(element);
        if (element > root.element)
            root.right = insert(element, root.right);
        else if(element < root.element)
            root.left = insert(element, root.left);
        else
            ;
        return root;
        /**
        if(Math.random() > 0.5)//insert a node randomly in left nodes
            root.left = insert(element, root.left);
        else
            root.right = insert(element, root.right);
        return root;
        */
    }
    
    public void printTree(BinaryNode root){
        if(root == null)
            return;
        Queue<BinaryNode> queue = new LinkedList<>();
        
        int current;//当前层 还未打印的结点个数
        int next;//下一层结点个数
        
        queue.offer(root);
        current = 1;
        next = 0;
        while(!queue.isEmpty()){
            BinaryNode currentNode = queue.poll();
            System.out.printf("%-4d", currentNode.element);
            current--;
            
            if(currentNode.left != null){
                queue.offer(currentNode.left);
                next++;
            }
            if(currentNode.right != null){
                queue.offer(currentNode.right);
                next++;
            }
            if(current ==0){
                System.out.println();
                current = next;
                next = 0;
            }
        }
    }
    
    
    public void findExpectedSumPath(BinaryNode root, int expectedSum){
        if(root == null)
            return;
        LinkedList<BinaryNode> stack = new LinkedList<BinaryNode>();
        int currentSum = 0;
        findExpectedSumPath(root, expectedSum, currentSum, stack);
    }
    private void findExpectedSumPath(BinaryNode root, int expectedSum, int currentSum, LinkedList<BinaryNode> stack){
        currentSum += root.element;
        stack.push(root);
        
        boolean isLeaf = (root.left == null && root.right == null);
        if(currentSum == expectedSum && isLeaf){//print path
            Iterator<BinaryNode> it = stack.descendingIterator();//逆序遍历List(Stack)
            while(it.hasNext())
                System.out.print(it.next().element + " ");
            
            System.out.println();
        }//end if
        
        if(root.left != null)
            findExpectedSumPath(root.left, expectedSum, currentSum, stack);
        if(root.right != null)
            findExpectedSumPath(root.right, expectedSum, currentSum, stack);
        
        stack.pop();//当某结点左右孩子均为null时, 回退
    }
    
    
    public void findExpectedSumPath2(BinaryNode root, int expectedSum){
        if(root == null)
            return;
        LinkedList<BinaryNode> stack = new LinkedList<BinaryNode>();
        int currentSum = 0;
        findExpectedSumPath2(root, expectedSum, currentSum, stack);
    }
    private void findExpectedSumPath2(BinaryNode root, int expectedSum, int currentSum, LinkedList<BinaryNode> stack){
        currentSum += root.element;
        stack.push(root);
        
//        boolean isLeaf = (root.left == null && root.right == null);
        if(currentSum == expectedSum){//print path
            Iterator<BinaryNode> it = stack.descendingIterator();//逆序遍历List(Stack)
            while(it.hasNext())
                System.out.print(it.next().element + " ");
            
            System.out.println();
        }//end if
        
        //当currentSum > expectedSum时 再往下递归没有意义了
        if(currentSum < expectedSum){
            if(root.left != null)
                findExpectedSumPath2(root.left, expectedSum, currentSum, stack);
            if(root.right != null)
                findExpectedSumPath2(root.right, expectedSum, currentSum, stack);
        }
        
        stack.pop();//当某结点左右孩子均为null时, 回退
    }
    
    public static void main(String[] args) {
        ExpectedSumPath tree = new ExpectedSumPath();
        int[] elements = {20,18,4,19,22};
        for (int i : elements) {
            tree.insert(i);
        }
        tree.findExpectedSumPath2(tree.root, 42);
    }
}
本文转自hapjin博客园博客,原文链接:http://www.cnblogs.com/hapjin/p/5565221.html,如需转载请自行联系原作者

相关文章:

  • css 背景属性详细总结
  • 使用Aspose.Cell控件实现Excel高难度报表的生成(二)
  • 第十四章 重载运算与类型转换
  • 使用类的思想来重构asp网站开发
  • android129 zhihuibeijing 聊天机器人
  • 【转】nios II架构uclinux的过程
  • 开发人员学Linux(4):使用JMeter对网站和数据库进行压力测试
  • 项目选题报告
  • [转载]精益求精Sybase数据库标题大包括-6
  • Android基础:SQLites数据库事物处理的优越性
  • DB2 9 利用斥地(733 测验)认证指南,第 9 部分: 用户定义的例程(2)
  • JS区别IE6、IE7、IE8之间的方法
  • 基础算法10:过滤器(Filter)对指定路径不进行过滤
  • Asp.net用户多次登录问题
  • 如何应对被地下的Oracle口令加密算法(1)
  • ES6指北【2】—— 箭头函数
  • @angular/forms 源码解析之双向绑定
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • github从入门到放弃(1)
  • Git同步原始仓库到Fork仓库中
  • JavaScript-Array类型
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • js学习笔记
  • Map集合、散列表、红黑树介绍
  • Mocha测试初探
  • python 装饰器(一)
  • Python打包系统简单入门
  • tensorflow学习笔记3——MNIST应用篇
  • Twitter赢在开放,三年创造奇迹
  • 飞驰在Mesos的涡轮引擎上
  • 精彩代码 vue.js
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 物联网链路协议
  • 学习笔记TF060:图像语音结合,看图说话
  • 容器镜像
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • (13)Hive调优——动态分区导致的小文件问题
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (二十四)Flask之flask-session组件
  • (分布式缓存)Redis分片集群
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (过滤器)Filter和(监听器)listener
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一一四)第九章编程练习
  • (转)详解PHP处理密码的几种方式
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .a文件和.so文件
  • .net 程序发生了一个不可捕获的异常
  • .net 发送邮件
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项