題目:輸入一棵二叉樹和一個整數,打印出二叉樹中結點值的和爲輸入整數的所有路徑。從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。
解題思路:當用前序遍歷的方式訪問到某一結點時,把該結點添加到路徑上,並累加該結點的值。如果該節點爲葉結點並且路徑中結點值的和剛好等於輸入的整數,則當前的路徑複覈要求,把它打印出來。如果當前結點不是葉結點,則繼續訪問它的子結點。當前結點訪問結束後,遞歸函數將自動回到它的父結點。因此在函數退出之前要在路徑上刪除當前結點並減去當前結點的值,以確保返回父結點時路徑剛好是從根結點到父結點的路徑。
C#實現:
public static void FindPath(BinaryTreeNode pRoot, int exceptedSum) { if (pRoot == null) return; List<int> path = new List<int>(); int currentSum = 0; FindPath(pRoot, exceptedSum, path, ref currentSum); } private static void FindPath(BinaryTreeNode pRoot, int exceptedSum, List<int> path, ref int currentSum) { currentSum += pRoot.value; path.Add(pRoot.value); // 如果是葉結點,並且路徑上結點的和等於輸入值 // 打印出這條路徑 bool isLeaf = pRoot.left == null && pRoot.right == null; if (currentSum == exceptedSum && isLeaf) { Console.WriteLine("A path is found: "); foreach (int item in path) Console.Write(item + "\t"); Console.WriteLine(); } // 如果不是葉結點,則遍歷它的子結點 if (pRoot.left != null) FindPath(pRoot.left, exceptedSum, path, ref currentSum); if (pRoot.right != null) FindPath(pRoot.right, exceptedSum, path, ref currentSum); // 在返回到父結點之前,在路徑上刪除當前結點, // 並在currentSum中減去當前結點的值 currentSum -= pRoot.value; path.RemoveAt(path.Count - 1); }
Java實現:
public static void findPath(BinaryTreeNode pRoot, int exceptedSum){ if(pRoot == null) return; List<Integer> path = new LinkedList<Integer>(); int currentSum = 0; findPath(pRoot, exceptedSum, path, (Integer)currentSum); } private static void findPath(BinaryTreeNode pRoot, int exceptedSum, List<Integer> path, Integer currentSum){ currentSum += pRoot.value; path.add(pRoot.value); // 如果是葉結點,並且路徑上結點的和等於輸入值 // 打印出這條路徑 boolean isLeaf = pRoot.left == null && pRoot.right == null; if(currentSum.intValue() == exceptedSum && isLeaf) { System.out.println("A path is found: "); for(int item: path) System.out.print(item + "\t"); System.out.println(); } // 如果不是葉節點,則遍歷它的子結點 if(pRoot.left != null) findPath(pRoot.left, exceptedSum, path, currentSum); if(pRoot.right != null) findPath(pRoot.right, exceptedSum, path, currentSum); // 在返回到父結點之前,在路徑上刪除當前結點, // 並在currentSum中減去當前結點的值 currentSum -= pRoot.value; path.remove(path.size()-1); }
Python實現:
@staticmethod def findPath(pRoot, exceptedSum): """ 二叉樹中和爲某一值的路徑 輸入一棵二叉樹和一個整數,打印出二叉樹中結點值的和爲輸入整數的所有路徑。 從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。 :param pRoot: :param exceptedSum: :return: """ if pRoot == None: return path = [] currentSum = [0,] BinaryTree.findPathChild(pRoot, exceptedSum, path, currentSum) @staticmethod def findPathChild(pRoot, exceptedSum, path, currentSum): currentSum[0] += pRoot.value path.append(pRoot.value) # 如果是葉結點,並且路徑上結點的和等於輸入值 # 打印出這條路徑 isLeaf = pRoot.left == None and pRoot.right == None if currentSum[0] == exceptedSum and isLeaf: print("A path is found:") for item in path: print(item, end=" ") print() # 如果不是葉節點,則遍歷它的子結點 if pRoot.left != None: BinaryTree.findPathChild(pRoot.left, exceptedSum, path, currentSum) if pRoot.right != None: BinaryTree.findPathChild(pRoot.right, exceptedSum, path, currentSum) # 在返回到父結點之前,在路徑上刪除當前結點, # 並在currentSum中減去當前結點的值 path.pop() currentSum[0] -= pRoot.value