日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构与算法-- 二叉树中和为某一值的路径

發布時間:2023/12/4 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构与算法-- 二叉树中和为某一值的路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二叉樹中和為某一值的路徑

  • 題目:輸入一顆二叉樹和一個整數,打印出二叉樹中節點值的和為給定值的所有路徑。從樹的根節點開始往下一只到葉子節點所經過的節點形成一條路徑。
  • 我們用二叉樹節點的定義沿用之前文章中 二叉查找樹實現原理定義。如下:
public class BinaryNode implements Comparable {private Object element;private BinaryNode left;private BinaryNode right;private int count;
  • 案例:輸入如下圖中二叉樹和整數22,打印出兩條路徑,第一條包含節點10, 12,第二條包含10,5,7

  • 二叉樹的節點定義中,我們是無法得到樹的路徑這個概念值的,應為二叉樹中并沒有介紹樹的路徑的數據結構,因此對于大多數人而言,這個是一個新的理論模型,也就很難一下找到思路,我們從一個案例入手更加簡單,還是用上面的案例:
  • 由于路徑就是根節點出發到葉子節點,也就是說路徑總是以根節點為起點,因此是先根遍歷,也就是我們可以用二叉樹的前序遍歷,根,左,右的遍歷方式,在我之前的文章 數據結構與算法–二叉樹實現原理 中詳細介紹了三種遍歷方式并附帶源碼,我們以前序遍歷為思路解這個問題。
  • 按前序遍歷范問上圖,10,5,4,7,12 是前序遍歷的順序,我們可以從二叉樹結構中看到,沒有指向父節點的指針,因此范問5 的時候并不知道10 節點的路徑和值,因此我們需要把上一個節點經過的路徑保存下來,每次范問一個節點都將當前節點的路徑添加到下一個節點的路徑中,因此到達5 時候,因存 10,5接著到大4,總和為9 != 22.因此返回5節點,到達7 ,和為 10+5+7 = 22 符合提議,接著返回10 ,訪問12,10+12 = 22 符合題意,因此有兩組解
  • 按如上分析有如下步驟:
    • 安前序遍歷遞歸實現二叉樹的遍歷
    • 范問每個子節點之前將本節點走過的路徑path和 路徑和count傳遞給需要范問的子節點
    • 字節點范問之前將父節點路徑path添加上自己的路徑 path+element,路徑和添加自己的節點權重 path+weight
    • 如果范問節點沒有子節點,則比較路徑和count 是否和目標 值一致,一致則正解。
    • 如下實現,此處構造二叉樹的方法沿用之前文章:數據結構與算法–二叉查找樹實現原理中方法
/*** 二叉樹中查找和為n 的路徑** @author liaojiamin* @Date:Created in 10:51 2021/5/17*/ public class FindPathInBinary {public static boolean findPath(BinaryNode binaryNode, Integer target) {List<Integer> list = new ArrayList<>();list.add(Integer.valueOf(binaryNode.getElement().toString()));Integer count = Integer.valueOf(binaryNode.getElement().toString());return findPath(binaryNode, list, count, target);}public static boolean findPath(BinaryNode binaryNode, List<Integer> beforePath, Integer beforeCount, Integer target) {if (binaryNode.getLeft() == null && binaryNode.getRight() == null) {printPath(beforePath);System.out.println("beforeCount:" + beforeCount);if(beforeCount == target){System.out.print("存在路徑:");printPath(beforePath);System.out.println();}return false;}if (binaryNode.getLeft() != null) {BinaryNode left = binaryNode.getLeft();Integer count = beforeCount + Integer.valueOf(left.getElement().toString());List<Integer> list = new ArrayList<>();list.addAll(beforePath);list.add(Integer.valueOf(left.getElement().toString()));findPath(binaryNode.getLeft(), list, count, target);}if(binaryNode.getRight() != null){BinaryNode right = binaryNode.getRight();Integer count = beforeCount + Integer.valueOf(right.getElement().toString());List<Integer> list = new ArrayList<>();list.addAll(beforePath);list.add(Integer.valueOf(right.getElement().toString()));findPath(binaryNode.getRight(), list, count, target);}return false;}public static void printPath(List<Integer> objectList) {for (Object o : objectList) {System.out.print(o);System.out.print(",");}}public static void main(String[] args) { // Integer arr[] = new Integer[]{44,94,92,23,42,13,7,76,70,40,78,28,78,36,14,53,10,91,36,15};Integer arr[] = new Integer[]{10,5,4,7,12};BinaryNode node = new BinaryNode(null, null, null);BinarySearchTree searchTree = new BinarySearchTree();for (int i = 0; i < arr.length; i++) {int num = arr[i];node = searchTree.insert(num, node);System.out.print(num+",");}System.out.println();System.out.println();findPath(node, 22);}}
  • 該問題考察的是二叉樹遍歷的變種,考察復雜問題的思維能力,當一下沒有思路,我們可以從具體案例開始分析,這樣就能找出其中規律

上一篇:數據結構與算法-- 二叉樹后續遍歷序列校驗
下一篇:數據結構與算法–復雜鏈表的復制

總結

以上是生活随笔為你收集整理的数据结构与算法-- 二叉树中和为某一值的路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。