仅具有左子节点的二叉树中的节点总和
给定一棵二叉树,任务是找到只有左子节点的二叉树节点的总和。
例子:
Input: 8
/ \
3 7
/ \ /
5 6 0
/ /
1 2
Output: 18
Explanation: Nodes with values 5, 6, and 7 are the ones that have only the left child nodes
Input: 2
/ \
3 1
/ /
5 6
Output: 4
方法:给定的问题可以通过使用后序遍历来遍历二叉树来解决。这个想法是检查一个节点是否只包含一个左子节点,如果条件为真,则将当前节点的值添加到答案中。可以按照以下步骤解决问题:
- 使用后序遍历遍历二叉树
- 如果根为空,则返回 0
- 在左子树上使用递归并将其答案存储在变量left
- 在右子树上使用递归并将其答案存储在变量right
- 如果root.left != null && root.right == null,则返回root.val + left + right的值
- 否则返回左+右
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
struct Node
{
int val;
Node *left;
Node *right;
Node(int value)
{
val = value;
left = NULL;
right = NULL;
}
};
// Function to find the sum of nodes
// having only left child node
int sumLeftChild(Node *root)
{
if (root == NULL)
return 0;
// Sum of nodes having only left
// child node from left subtree
int left = sumLeftChild(root->left);
// Sum of nodes having only left
// child node from right subtree
int right = sumLeftChild(root->right);
// If current node has only left
// child node return the sum from
// left subtree + right
// subtree + root->val
if (root->left != NULL && root->right == NULL)
{
return root->val + left + right;
}
// Return the value from left
// and right subtrees
return left + right;
}
// Driver code
int main()
{
// Initialize the tree
Node *root = new Node(2);
root->left = new Node(3);
root->right = new Node(4);
root->left->left = new Node(5);
root->right->left = new Node(7);
cout << (sumLeftChild(root));
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the sum of nodes
// having only left child node
public static int sumLeftChild(Node root)
{
if (root == null)
return 0;
// Sum of nodes having only left
// child node from left subtree
int left = sumLeftChild(root.left);
// Sum of nodes having only left
// child node from right subtree
int right = sumLeftChild(root.right);
// If current node has only left
// child node return the sum from
// left subtree + right
// subtree + root.val
if (root.left != null && root.right == null) {
return root.val + left + right;
}
// Return the value from left
// and right subtrees
return left + right;
}
// Driver code
public static void main(String[] args)
{
// Initialize the tree
Node root = new Node(2);
root.left = new Node(3);
root.right = new Node(4);
root.left.left = new Node(5);
root.right.left = new Node(7);
System.out.println(sumLeftChild(root));
}
static class Node {
int val;
Node left, right;
public Node(int val)
{
this.val = val;
}
}
}
C#
// C# implementation for the above approach
using System;
public class GFG {
// Function to find the sum of nodes
// having only left child node
public static int sumLeftChild(Node root) {
if (root == null)
return 0;
// Sum of nodes having only left
// child node from left subtree
int left = sumLeftChild(root.left);
// Sum of nodes having only left
// child node from right subtree
int right = sumLeftChild(root.right);
// If current node has only left
// child node return the sum from
// left subtree + right
// subtree + root.val
if (root.left != null && root.right == null) {
return root.val + left + right;
}
// Return the value from left
// and right subtrees
return left + right;
}
// Driver code
public static void Main(String[] args) {
// Initialize the tree
Node root = new Node(2);
root.left = new Node(3);
root.right = new Node(4);
root.left.left = new Node(5);
root.right.left = new Node(7);
Console.WriteLine(sumLeftChild(root));
}
public class Node {
public int val;
public Node left, right;
public Node(int val) {
this.val = val;
}
}
}
// This code is contributed by Rajput-Ji
Javascript
输出
7
时间复杂度: O(N)
辅助空间: O(1)