劍指offer第二版-34.二叉樹中和為某一值的路徑

本系列導(dǎo)航:劍指offer(第二版)java實(shí)現(xiàn)導(dǎo)航帖

面試題34:二叉樹中和為某一值的路徑

題目要求:
輸入一棵二叉樹和一個(gè)整數(shù),打印出二叉樹中節(jié)點(diǎn)值的和為輸入整數(shù)的所有路徑。從樹的根節(jié)點(diǎn)開始往下一直到葉節(jié)點(diǎn)所經(jīng)過的節(jié)點(diǎn)形成一條路徑。

解題思路:
需要得到所有從根節(jié)點(diǎn)到葉節(jié)點(diǎn)的路徑,判斷和是否為給定值。自己寫一個(gè)小的二叉樹,通過模擬上述過程,發(fā)現(xiàn)獲取所有路徑的過程與前序遍歷類似。
因此,可以對給定二叉樹進(jìn)行前序遍歷。當(dāng)被訪問節(jié)點(diǎn)時(shí)葉節(jié)點(diǎn)時(shí),判斷路徑和是否為給定值。此外,為了記錄路徑上的節(jié)點(diǎn),需要一個(gè)數(shù)組。

package chapter4;
import structure.TreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ryder on 2017/7/18.
 * 二叉樹中和為某一值的路徑
 */
public class P182_FindPath {
    //用類似于前序遍歷的思路解決
    public static void findPath(TreeNode<Integer> root,int exceptedSum){
        if(root==null)
            return;
        List<Integer> path = new ArrayList<>();
        findPath(root,path,exceptedSum,0);
    }
    //curNode為將要被訪問的節(jié)點(diǎn),還未被加入到path中
    public static void findPath(TreeNode<Integer> curNode,List<Integer> path,int exceptedSum,int currentSum){
        path.add(curNode.val);
        currentSum+=curNode.val;
        if(curNode.left!=null)
            findPath(curNode.left,path,exceptedSum,currentSum);
        if(curNode.right!=null)
            findPath(curNode.right,path,exceptedSum,currentSum);
        if(curNode.left==null && curNode.right==null && currentSum==exceptedSum)
            System.out.println(path);
        path.remove(path.size()-1) ;
    }
   
    public static void main(String[] args) {
        //            10
        //          /   \
        //         5     12
        //       /  \
        //      4    7
        TreeNode<Integer> root = new TreeNode<Integer>(10);
        root.left = new TreeNode<Integer>(5);
        root.right = new TreeNode<Integer>(12);
        root.left.left = new TreeNode<Integer>(4);
        root.left.right = new TreeNode<Integer>(7);
        findPath(root,22);
        findPath2(root,22);
    }
}

如果二叉樹的所有節(jié)點(diǎn)值都是大于0的(原題中并沒有這個(gè)條件),可以進(jìn)行剪枝。

//如果所有節(jié)點(diǎn)值均大于0,可進(jìn)行剪枝
    public static void findPath2(TreeNode<Integer> root,int exceptedSum){
        if(root==null)
            return;
        List<Integer> path = new ArrayList<>();
        findPath2(root,path,exceptedSum,0);
    }
     //curNode為將要被訪問的節(jié)點(diǎn),還未被加入到path中
    public static void findPath2(TreeNode<Integer> curNode,List<Integer> path,int exceptedSum,int currentSum){
        path.add(curNode.val);
        currentSum+=curNode.val;
        //只有當(dāng)currentSum小于exceptedSum時(shí)需要繼續(xù)當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)的遍歷
        if(currentSum<exceptedSum){
            if(curNode.left!=null)
                findPath2(curNode.left,path,exceptedSum,currentSum);
            if(curNode.right!=null)
                findPath2(curNode.right,path,exceptedSum,currentSum);
        }
        //currentSum大于等于exceptedSum時(shí)可以直接停止當(dāng)前分支的遍歷,因?yàn)楫?dāng)前分支下currentSum只會(huì)越來越大,不會(huì)再有符合要求的解
        else if(currentSum==exceptedSum && curNode.left==null && curNode.right==null)
            System.out.println(path);
        path.remove(path.size()-1) ;
    }

運(yùn)行結(jié)果

[10, 5, 7]
[10, 12]
[10, 5, 7]
[10, 12]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 樹的概述 樹是一種非常常用的數(shù)據(jù)結(jié)構(gòu),樹與前面介紹的線性表,棧,隊(duì)列等線性結(jié)構(gòu)不同,樹是一種非線性結(jié)構(gòu) 1.樹的定...
    Jack921閱讀 4,489評論 1 31
  • 面試題7:重建二叉樹 題目: 輸入某二叉樹的前序遍歷和中序遍歷的結(jié)果。請重建該二叉樹。假設(shè)輸入的前序遍歷和中序遍歷...
    lyoungzzz閱讀 575評論 0 0
  • 基于樹實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu),具有兩個(gè)核心特征: 邏輯結(jié)構(gòu):數(shù)據(jù)元素之間具有層次關(guān)系; 數(shù)據(jù)運(yùn)算:操作方法具有Log級(jí)的平...
    yhthu閱讀 4,320評論 1 5
  • 二叉樹中和為某一值的路徑 題目描述 輸入一顆二叉樹和一個(gè)整數(shù),打印出二叉樹中結(jié)點(diǎn)值的和為輸入整數(shù)的所有路徑。路徑定...
    echoVic閱讀 1,619評論 1 3
  • 0. 什么是樹 數(shù)據(jù)的基本單位是數(shù)據(jù)元素,在涉及到數(shù)據(jù)處理時(shí)數(shù)據(jù)元素之間的關(guān)系稱之為結(jié)構(gòu),我們依據(jù)數(shù)據(jù)元素之間關(guān)系...
    安安zoe閱讀 498評論 0 0