Dado un árbol binario y dos Nodes, la tarea es verificar si los Nodes son hermanos entre sí o no.
Se dice que dos Nodes son hermanos si están presentes en el mismo nivel y sus padres son iguales.
Ejemplos:
Input : 1 / \ 2 3 / \ / \ 4 5 6 7 First node is 4 and Second node is 6. Output : No, they are not siblings. Input : 1 / \ 5 6 / / \ 7 3 4 First node is 3 and Second node is 4 Output : Yes
Enfoque: al observar detenidamente, se puede concluir que cualquier Node en un árbol binario puede tener un máximo de dos Nodes secundarios. Entonces, dado que el padre de dos hermanos debe ser el mismo, la idea es simplemente atravesar el árbol y para cada Node verificar si los dos Nodes dados son sus hijos. Si es cierto para cualquier Node en el árbol, imprima SÍ; de lo contrario, imprima NO.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to check if two nodes are // siblings #include <bits/stdc++.h> using namespace std; // Binary Tree Node struct Node { int data; Node *left, *right; }; // Utility function to create a new node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Function to find out if two nodes are siblings bool CheckIfNodesAreSiblings(Node* root, int data_one, int data_two) { if (!root) return false; // Compare the two given nodes with // the childrens of current node if (root->left && root->right) { int left = root->left->data; int right = root->right->data; if (left == data_one && right == data_two) return true; else if (left == data_two && right == data_one) return true; } // Check for left subtree if (root->left) if(CheckIfNodesAreSiblings(root->left, data_one, data_two)) return true; // Check for right subtree if (root->right) if(CheckIfNodesAreSiblings(root->right, data_one, data_two)) return true; } // Driver code int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->right->left = newNode(5); root->right->right = newNode(6); root->left->left->right = newNode(7); int data_one = 5; int data_two = 6; if (CheckIfNodesAreSiblings(root, data_one, data_two)) cout << "YES"; else cout << "NO"; return 0; }
Java
// Java program to check if two nodes // are siblings import java.util.*; class GFG{ // Binary Tree Node static class Node { int data; Node left, right; }; // Utility function to create a // new node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = null; return (node); } static Node root = null; // Function to find out if two nodes are siblings static boolean CheckIfNodesAreSiblings(Node root, int data_one, int data_two) { if (root == null) return false; // Compare the two given nodes with // the childrens of current node if (root.left != null && root.right != null) { int left = root.left.data; int right = root.right.data; if (left == data_one && right == data_two) return true; else if (left == data_two && right == data_one) return true; } // Check for left subtree if (root.left != null) CheckIfNodesAreSiblings(root.left, data_one, data_two); // Check for right subtree if (root.right != null) CheckIfNodesAreSiblings(root.right, data_one, data_two); return true; } // Driver code public static void main(String[] args) { root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.right.left = newNode(5); root.right.right = newNode(6); root.left.left.right = newNode(7); int data_one = 5; int data_two = 6; if (CheckIfNodesAreSiblings(root, data_one, data_two)) System.out.print("YES"); else System.out.print("NO"); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to check if two # nodes are siblings # Binary Tree Node class Node: def __init__(self, key): self.data = key self.left = None self.right = None # Function to find out if two nodes are siblings def CheckIfNodesAreSiblings(root, data_one, data_two): if (root == None): return False # Compare the two given nodes with # the childrens of current node ans = False if (root.left != None and root.right != None): left = root.left.data right = root.right.data if (left == data_one and right == data_two): return True elif (left == data_two and right == data_one): return True # Check for left subtree if (root.left != None): ans = ans or CheckIfNodesAreSiblings(root.left, data_one, data_two) # Check for right subtree if (root.right != None): ans = ans or CheckIfNodesAreSiblings(root.right, data_one, data_two) return ans # Driver code if __name__ == '__main__': root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.right.left = Node(5) root.right.right = Node(6) root.left.left.right = Node(7) data_one = 5 data_two = 6 if (CheckIfNodesAreSiblings(root, data_one, data_two)): print("YES") else: print("NO") # This code is contributed by mohit kumar 29
C#
// C# program to check if two nodes // are siblings using System; // Binary Tree Node public class Node { public int data; public Node left, right; // Utility function to create a // new node public Node(int item) { data = item; left = right = null; } } class GFG{ static Node root = null; // Function to find out if two // nodes are siblings static bool CheckIfNodesAreSiblings(Node root, int data_one, int data_two) { if (root == null) { return false; } // Compare the two given nodes with // the childrens of current node if (root.left != null && root.right != null) { int left = root.left.data; int right = root.right.data; if (left == data_one && right == data_two) { return true; } else if (left == data_two && right == data_one) { return true; } } // Check for left subtree if (root.left != null) { CheckIfNodesAreSiblings(root.left, data_one, data_two); } // Check for right subtree if (root.right != null) { CheckIfNodesAreSiblings(root.right, data_one, data_two); } return true; } // Driver code static public void Main() { GFG.root = new Node(1); GFG.root.left = new Node(2); GFG.root.right = new Node(3); GFG.root.left.left = new Node(4); GFG.root.right.left = new Node(5); GFG.root.right.right = new Node(6); GFG.root.left.left.right = new Node(7); int data_one = 5; int data_two = 6; if (CheckIfNodesAreSiblings(root, data_one, data_two)) { Console.WriteLine("YES"); } else { Console.WriteLine("NO"); } } } // This code is contributed by avanitrachhadiya2155
Javascript
<script> // Javascript program to check if two nodes // are siblings // Binary Tree Node class Node { constructor(data) { this.data = data; this.left = this.right = null; } } let root = null; // Function to find out if two nodes are siblings function CheckIfNodesAreSiblings( root, data_one, data_two) { if (root == null) return false; // Compare the two given nodes with // the childrens of current node if (root.left != null && root.right != null) { let left = root.left.data; let right = root.right.data; if (left == data_one && right == data_two) return true; else if (left == data_two && right == data_one) return true; } // Check for left subtree if (root.left != null) CheckIfNodesAreSiblings(root.left, data_one, data_two); // Check for right subtree if (root.right != null) CheckIfNodesAreSiblings(root.right, data_one, data_two); return true; } // Driver code root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.right.left = new Node(5); root.right.right = new Node(6); root.left.left.right = new Node(7); let data_one = 5; let data_two = 6; if (CheckIfNodesAreSiblings(root, data_one, data_two)) document.write("YES"); else document.write("NO"); // This code is contributed by unknown2108 </script>
YES
Publicación traducida automáticamente
Artículo escrito por Sakshi_Srivastava y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA