ในปัญหานี้ เราได้รับไบนารีทรี งานของเราคือ หาผลรวมของใบไม้ที่เหลือทั้งหมดในต้นไม้ไบนารีที่กำหนด .
มาดูตัวอย่างเพื่อทำความเข้าใจปัญหากัน
ป้อนข้อมูล :
เอาท์พุต :11
คำอธิบาย −
All leaf nodes of the tree are : 2, 9 Sum = 2 + 9 = 11
แนวทางการแก้ปัญหา
วิธีแก้ปัญหาง่ายๆ คือการสำรวจต้นไม้จากรากหนึ่งไปยังอีกใบหนึ่ง หากโหนดเป็นโหนดปลายด้านซ้าย ให้เพิ่มลงในผลรวม เมื่อข้ามต้นไม้ทั้งต้น พิมพ์ผลรวม
ตัวอย่าง
โปรแกรมเพื่อแสดงการทำงานของโซลูชันของเรา
#include <iostream>
using namespace std;
struct Node{
int key;
struct Node* left, *right;
};
Node *newNode(char k){
Node *node = new Node;
node->key = k;
node->right = node->left = NULL;
return node;
}
bool isLeafNode(Node *node){
if (node == NULL)
return false;
if (node->left == NULL && node->right == NULL)
return true;
return false;
}
int findLeftLeavesSum(Node *root){
int sum = 0;
if (root != NULL){
if (isLeafNode(root->left))
sum += root->left->key;
else
sum += findLeftLeavesSum(root->left);
sum += findLeftLeavesSum(root->right);
}
return sum;
}
int main(){
struct Node *root = newNode(5);
root->left = newNode(4);
root->right = newNode(6);
root->left->left = newNode(2);
root->left->right = newNode(1);
root->right->left = newNode(9);
root->right->right = newNode(7);
cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root);
return 0;
} ผลลัพธ์
The sum of left leaves of the tree is 11
แนวทางอื่นโดยใช้การวนซ้ำ
เราจะทำการค้นหาเชิงลึกก่อนข้ามผ่านต้นไม้ จากนั้นตรวจสอบว่าโหนดปัจจุบันเป็นใบไม้ด้านซ้ายหรือไม่ ถ้าใช่ ให้เพิ่มมูลค่าเป็นผลรวม มิฉะนั้น ให้ปล่อย ในตอนท้ายพิมพ์ผลรวม
ตัวอย่าง
โปรแกรมเพื่อแสดงการทำงานของโซลูชันของเรา
#include<bits/stdc++.h>
using namespace std;
struct Node{
int key;
struct Node* left, *right;
};
Node *newNode(char k){
Node *node = new Node;
node->key = k;
node->right = node->left = NULL;
return node;
}
int findLeftLeavesSum(Node* root){
if(root == NULL)
return 0;
stack<Node*> treeNodes;
treeNodes.push(root);
int sum = 0;
while(treeNodes.size() > 0){
Node* currentNode = treeNodes.top();
treeNodes.pop();
if (currentNode->left != NULL){
treeNodes.push(currentNode->left);
if(currentNode->left->left == NULL &&
currentNode->left->right == NULL){
sum += currentNode->left->key ;
}
}
if (currentNode->right != NULL)
treeNodes.push(currentNode->right);
}
return sum;
}
int main(){
Node *root = newNode(5);
root->left= newNode(4);
root->right = newNode(6);
root->left->left = newNode(2);
root->left->right = newNode(1);
root->right->left = newNode(9);
root->right->right= newNode(7);
cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root);
return 0;
} ผลลัพธ์
The sum of left leaves of the tree is 11
วิธีที่ 3 โดยใช้ BFS
เราจะทำการค้นหาในวงกว้างก่อนด้วยตัวแปรเพื่อระบุว่าโหนดนั้นเป็นโหนดลูกหรือไม่ ถ้าใช่ ให้บวกกับผลรวม อย่างอื่นปล่อย ในตอนท้ายพิมพ์ผลรวม
ตัวอย่าง
โปรแกรมเพื่อแสดงการทำงานของโซลูชันของเรา
#include<bits/stdc++.h>
using namespace std;
struct Node{
int key; struct Node* left, *right;
};
Node *newNode(char k){
Node *node = new Node;
node->key = k;
node->right = node->left = NULL;
return node;
}
int findLeftLeavesSum(Node* root) {
if (root == NULL)
return 0;
queue<pair<Node*, bool> > leftTreeNodes;
leftTreeNodes.push({ root, 0 });
int sum = 0;
while (!leftTreeNodes.empty()) {
Node* temp = leftTreeNodes.front().first;
bool is_left_child = leftTreeNodes.front().second;
leftTreeNodes.pop();
if (!temp->left && !temp->right && is_left_child)
sum = sum + temp->key;
if (temp->left) {
leftTreeNodes.push({ temp->left, 1 });
}
if (temp->right) {
leftTreeNodes.push({ temp->right, 0 });
}
}
return sum;
}
int main(){
Node *root = newNode(5);
root->left= newNode(4);
root->right = newNode(6);
root->left->left = newNode(2);
root->left->right = newNode(1);
root->right->left = newNode(9);
root->right->right= newNode(7);
cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root);
return 0;
} ผลลัพธ์
The sum of left leaves of the tree is 11