Dado un árbol binario, la tarea es encontrar la suma de los Nodes que son visibles en la vista izquierda. La vista izquierda de un árbol binario es el conjunto de Nodes visibles cuando el árbol se ve desde la izquierda.
Ejemplos:
Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 7 1 + 2 + 4 = 7 Input: 1 / \ 2 3 \ 4 \ 5 \ 6 Output: 18
Enfoque: El problema se puede resolver mediante un recorrido recursivo simple. Podemos realizar un seguimiento del nivel de un Node pasando un parámetro a todas las llamadas recursivas. La idea es realizar un seguimiento del nivel máximo también y atravesar el árbol de manera que el subárbol izquierdo se visite antes que el subárbol derecho. Cada vez que se encuentre un Node cuyo nivel sea mayor que el nivel máximo hasta el momento, agregue el valor del Node a la suma porque es el primer Node en su nivel (tenga en cuenta que el subárbol izquierdo se atraviesa antes que el subárbol derecho).
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *left, *right; }; // A utility function to create // a new Binary Tree Node Node* newNode(int item) { Node* temp = new Node(); temp->data = item; temp->left = temp->right = NULL; return temp; } // Recursive function to find the sum of nodes // of the left view of the given binary tree void sumLeftViewUtil(Node* root, int level, int* max_level, int* sum) { // Base Case if (root == NULL) return; // If this is the first Node of its level if (*max_level < level) { *sum += root->data; *max_level = level; } // Recur for left and right subtrees sumLeftViewUtil(root->left, level + 1, max_level, sum); sumLeftViewUtil(root->right, level + 1, max_level, sum); } // A wrapper over sumLeftViewUtil() void sumLeftView(Node* root) { int max_level = 0; int sum = 0; sumLeftViewUtil(root, 1, &max_level, &sum); cout << sum; } // Driver code int main() { Node* root = newNode(12); root->left = newNode(10); root->right = newNode(30); root->right->left = newNode(25); root->right->right = newNode(40); sumLeftView(root); return 0; }
Java
// Java implementation of the approach // Class for a node of the tree class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } } class BinaryTree { Node root; static int max_level = 0; static int sum = 0; // Recursive function to find the sum of nodes // of the left view of the given binary tree void sumLeftViewUtil(Node node, int level) { // Base Case if (node == null) return; // If this is the first node of its level if (max_level < level) { sum += node.data; max_level = level; } // Recur for left and right subtrees sumLeftViewUtil(node.left, level + 1); sumLeftViewUtil(node.right, level + 1); } // A wrapper over sumLeftViewUtil() void sumLeftView() { sumLeftViewUtil(root, 1); System.out.print(sum); } // Driver code public static void main(String args[]) { BinaryTree tree = new BinaryTree(); tree.root = new Node(12); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.right.left = new Node(25); tree.root.right.right = new Node(40); tree.sumLeftView(); } }
Python3
# Python3 implementation of the approach # A binary tree node class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None # Recursive function to find the sum of nodes # of the left view of the given binary tree def sumLeftViewUtil(root, level, max_level, sum): # Base Case if root is None: return # If this is the first node of its level if (max_level[0] < level): sum[0]+= root.data max_level[0] = level # Recur for left and right subtree sumLeftViewUtil(root.left, level + 1, max_level, sum) sumLeftViewUtil(root.right, level + 1, max_level, sum) # A wrapper over sumLeftViewUtil() def sumLeftView(root): max_level = [0] sum =[0] sumLeftViewUtil(root, 1, max_level, sum) print(sum[0]) # Driver code root = Node(12) root.left = Node(10) root.right = Node(20) root.right.left = Node(25) root.right.right = Node(40) sumLeftView(root)
C#
// C# implementation of the approach using System; // Class for a node of the tree public class Node { public int data; public Node left, right; public Node(int item) { data = item; left = right = null; } } public class BinaryTree { public Node root; public static int max_level = 0; public static int sum = 0; // Recursive function to find the sum of nodes // of the left view of the given binary tree public virtual void leftViewUtil(Node node, int level) { // Base Case if (node == null) { return; } // If this is the first node of its level if (max_level < level) { sum += node.data; max_level = level; } // Recur for left and right subtrees leftViewUtil(node.left, level + 1); leftViewUtil(node.right, level + 1); } // A wrapper over leftViewUtil() public virtual void leftView() { leftViewUtil(root, 1); Console.Write(sum); } // Driver code public static void Main(string[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(12); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.right.left = new Node(25); tree.root.right.right = new Node(40); tree.leftView(); } }
Javascript
<script> // Javascript implementation of the approach // Class for a node of the tree class Node { constructor(item) { this.data = item; this.left = null; this.right = null; } } var root = null; var max_level = 0; var sum = 0; // Recursive function to find the sum of nodes // of the left view of the given binary tree function leftViewUtil(node, level) { // Base Case if (node == null) { return; } // If this is the first node of its level if (max_level < level) { sum += node.data; max_level = level; } // Recur for left and right subtrees leftViewUtil(node.left, level + 1); leftViewUtil(node.right, level + 1); } // A wrapper over leftViewUtil() function leftView() { leftViewUtil(root, 1); document.write(sum); } // Driver code root = new Node(12); root.left = new Node(10); root.right = new Node(30); root.right.left = new Node(25); root.right.right = new Node(40); leftView(); // This code is contributed by rrrtnx. </script>
47
Complejidad de tiempo : O (N) donde N es el número de Nodes en un árbol binario dado
Espacio Auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA