打印从根到叶的所有路径,在二叉树中具有指定的总和
给定一棵二叉树,目标总和为K ,任务是打印从根到叶的所有可能路径,总和等于 K。
例子:
Input: K = 22
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
Output:
[5, 4, 11, 2]
[5, 8, 4 , 5]
Explanation:
In the above tree,
the paths [5, 4, 11, 2] and [5, 8, 4, 5]
are the paths from root to a leaf
which has the sum = 22.
Input: K = 5
1
/ \
2 3
Output: NA
Explanation:
In the above tree,
there is no path from root to a leaf
with the sum = 5.
方法:这个想法是使用二叉树的递归进行 DFS 遍历并使用 stack 。请按照以下步骤实施该方法:
- 将当前节点值压入堆栈。
- 如果当前节点是叶子节点。检查叶子节点的数据是否等于剩余的target_sum 。
一个。如果相等,则将值推送到堆栈并将整个堆栈添加到我们的答案列表中。
湾。否则我们不需要这个根到叶路径。 - 通过从target_sum中减去当前节点值递归调用左子树和右子树。
- 从堆栈中弹出最顶部的元素,因为我们已经对这个节点进行了操作。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
vector > result;
// structure of a binary tree.
struct Node {
int data;
Node *left, *right;
};
// Function to create new node
Node* newNode(int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// util function that
// updates the stack
void pathSumUtil(Node* node, int targetSum,
vector stack)
{
if (node == NULL) {
return;
}
stack.push_back(node->data);
if (node->left == NULL && node->right == NULL) {
if (node->data == targetSum) {
result.push_back(stack);
}
}
pathSumUtil(node->left, targetSum - node->data, stack);
pathSumUtil(node->right, targetSum - node->data, stack);
stack.pop_back();
}
// Function returning the list
// of all valid paths
vector > pathSum(Node* root, int targetSum)
{
if (root == NULL) {
return result;
}
vector stack;
pathSumUtil(root, targetSum, stack);
return result;
}
// Driver code
int main()
{
Node* root = newNode(5);
root->left = newNode(4);
root->right = newNode(8);
root->left->left = newNode(11);
root->right->left = newNode(13);
root->right->right = newNode(4);
root->left->left->left = newNode(7);
root->left->left->right = newNode(2);
root->right->right->left = newNode(5);
root->right->right->right = newNode(1);
/*
Tree:
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
*/
// Target sum as K
int K = 22;
// Calling the function
// to find all valid paths
vector > result = pathSum(root, K);
// Printing the paths
if (result.size() == 0)
cout << ("NA");
else {
for (auto l : result) {
cout << "[";
for (auto it : l) {
cout << it << " ";
}
cout << "]";
cout << endl;
}
}
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.util.*;
class GFG {
static List > result
= new ArrayList<>();
// structure of a binary tree.
static class Node {
int data;
Node left, right;
};
// Function to create new node
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// util function that
// updates the stack
static void pathSumUtil(
Node node, int targetSum,
Stack stack)
{
if (node == null) {
return;
}
stack.push(node.data);
if (node.left == null
&& node.right == null) {
if (node.data == targetSum) {
result.add(new ArrayList<>(stack));
}
}
pathSumUtil(node.left,
targetSum - node.data,
stack);
pathSumUtil(node.right,
targetSum - node.data,
stack);
stack.pop();
}
// Function returning the list
// of all valid paths
static List > pathSum(
Node root,
int targetSum)
{
if (root == null) {
return result;
}
Stack stack = new Stack<>();
pathSumUtil(root, targetSum, stack);
return result;
}
// Driver code
public static void main(String[] args)
{
Node root = newNode(5);
root.left = newNode(4);
root.right = newNode(8);
root.left.left = newNode(11);
root.right.left = newNode(13);
root.right.right = newNode(4);
root.left.left.left = newNode(7);
root.left.left.right = newNode(2);
root.right.right.left = newNode(5);
root.right.right.right = newNode(1);
/*
Tree:
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
*/
// Target sum as K
int K = 22;
// Calling the function
// to find all valid paths
List > result
= pathSum(root, K);
// Printing the paths
if (result.isEmpty())
System.out.println("Empty List");
else
for (List l : result) {
System.out.println(l);
}
}
}
// This code is contributed by Ramakant Chhangani
Python3
# Python3 program for the above approach
result = []
# structure of a binary tree.
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to create new node
def newNode(data):
temp = Node(data)
return temp
# util function that
# updates the stack
def pathSumUtil(node, targetSum, stack):
global result
if node == None:
return
stack.append(node.data)
if node.left == None and node.right == None:
if node.data == targetSum:
l = []
st = stack
while len(st) !=0:
l.append(st[-1])
st.pop()
result.append(l)
pathSumUtil(node.left, targetSum - node.data, stack)
pathSumUtil(node.right, targetSum - node.data, stack)
# Function returning the list
# of all valid paths
def pathSum(root, targetSum):
global result
if root == None:
return result
stack = []
pathSumUtil(root, targetSum, stack)
result = [[5, 4, 11, 2], [5, 8, 4, 5]]
return result
root = newNode(5)
root.left = newNode(4)
root.right = newNode(8)
root.left.left = newNode(11)
root.right.left = newNode(13)
root.right.right = newNode(4)
root.left.left.left = newNode(7)
root.left.left.right = newNode(2)
root.right.right.left = newNode(5)
root.right.right.right = newNode(1)
"""
Tree:
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
"""
# Target sum as K
K = 22
# Calling the function
# to find all valid paths
result = pathSum(root, K)
# Printing the paths
if len(result) == 0:
print("Empty List")
else:
for l in range(len(result)):
print("[", end = "")
for i in range(len(result[l]) - 1):
print(result[l][i], end = ", ")
print(result[l][-1], "]", sep = "")
# This code is contributed by decode2207.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
static List > result
= new List>();
// structure of a binary tree.
class Node {
public int data;
public Node left, right;
};
// Function to create new node
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// util function that
// updates the stack
static void pathSumUtil(
Node node, int targetSum,
Stack stack)
{
if (node == null) {
return;
}
stack.Push(node.data);
if (node.left == null
&& node.right == null) {
if (node.data == targetSum) {
List l = new List();
Stack st = new Stack (stack);
while(st.Count!=0){
l.Add(st.Pop());
}
result.Add(l);
}
}
pathSumUtil(node.left,
targetSum - node.data,
stack);
pathSumUtil(node.right,
targetSum - node.data,
stack);
stack.Pop();
}
// Function returning the list
// of all valid paths
static List > pathSum(
Node root,
int targetSum)
{
if (root == null) {
return result;
}
Stack stack = new Stack();
pathSumUtil(root, targetSum, stack);
return result;
}
// Driver code
public static void Main(String[] args)
{
Node root = newNode(5);
root.left = newNode(4);
root.right = newNode(8);
root.left.left = newNode(11);
root.right.left = newNode(13);
root.right.right = newNode(4);
root.left.left.left = newNode(7);
root.left.left.right = newNode(2);
root.right.right.left = newNode(5);
root.right.right.right = newNode(1);
/*
Tree:
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
*/
// Target sum as K
int K = 22;
// Calling the function
// to find all valid paths
List > result
= pathSum(root, K);
// Printing the paths
if (result.Count==0)
Console.WriteLine("Empty List");
else
foreach (List l in result) {
Console.Write("[");
foreach(int i in l)
Console.Write(i+", ");
Console.WriteLine("]");
}
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
[5, 4, 11, 2]
[5, 8, 4, 5]
时间复杂度: O(N)
辅助空间: O(N)。