Dado un árbol binario, imprima Nodes de nivel impar en cualquier orden. La raíz se considera en el nivel 1.
For example consider the following tree 1 / \ 2 3 / \ \ 4 5 6 / \ / 7 8 9 Output 1 4 5 6
Método 1 (recursivo): la idea es pasar el nivel inicial como impar y cambiar el indicador de nivel en cada llamada recursiva. Para cada Node, si se establece un indicador impar, imprímalo.
C++
// Recursive C++ program to print odd level nodes #include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left, *right; }; void printOddNodes(Node *root, bool isOdd = true) { // If empty tree if (root == NULL) return; // If current node is of odd level if (isOdd) cout << root->data << " " ; // Recur for children with isOdd // switched. printOddNodes(root->left, !isOdd); printOddNodes(root->right, !isOdd); } // Utility method to create a node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Driver code int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printOddNodes(root); return 0; }
Java
// Recursive Java program to print odd level nodes class GfG { static class Node { int data; Node left, right; } static void printOddNodes(Node root, boolean isOdd) { // If empty tree if (root == null) return; // If current node is of odd level if (isOdd == true) System.out.print(root.data + " "); // Recur for children with isOdd // switched. printOddNodes(root.left, !isOdd); printOddNodes(root.right, !isOdd); } // Utility method to create a node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver code public static void main(String[] args) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); printOddNodes(root, true); } }
Python3
# Recursive Python3 program to print # odd level nodes # Utility method to create a node class newNode: def __init__(self, data): self.data = data self.left = self.right = None def printOddNodes(root, isOdd = True): # If empty tree if (root == None): return # If current node is of odd level if (isOdd): print(root.data, end = " ") # Recur for children with isOdd # switched. printOddNodes(root.left, not isOdd) printOddNodes(root.right, not isOdd) # Driver code if __name__ == '__main__': root = newNode(1) root.left = newNode(2) root.right = newNode(3) root.left.left = newNode(4) root.left.right = newNode(5) printOddNodes(root) # This code is contributed by PranchalK
C#
using System; // Recursive C# program to print odd level nodes public class GfG { public class Node { public int data; public Node left, right; } public static void printOddNodes(Node root, bool isOdd) { // If empty tree if (root == null) { return; } // If current node is of odd level if (isOdd == true) { Console.Write(root.data + " "); } // Recur for children with isOdd // switched. printOddNodes(root.left, !isOdd); printOddNodes(root.right, !isOdd); } // Utility method to create a node public static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver code public static void Main(string[] args) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); printOddNodes(root, true); } } // This code is contributed by Shrikant13
Javascript
<script> // Recursive JavaScript program to print odd level nodes class Node { constructor(data) { this.left = null; this.right = null; this.data = data; } } function printOddNodes(root, isOdd) { // If empty tree if (root == null) return; // If current node is of odd level if (isOdd == true) document.write(root.data + " "); // Recur for children with isOdd // switched. printOddNodes(root.left, !isOdd); printOddNodes(root.right, !isOdd); } // Utility method to create a node function newNode(data) { let node = new Node(data); return (node); } let root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); printOddNodes(root, true); </script>
C++
// Iterative C++ program to print odd level nodes #include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left, *right; }; // Iterative method to do level order traversal line by line void printOddNodes(Node *root) { // Base Case if (root == NULL) return; // Create an empty queue for level // order traversal queue<Node *> q; // Enqueue root and initialize level as odd q.push(root); bool isOdd = true; while (1) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.size(); if (nodeCount == 0) break; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node *node = q.front(); if (isOdd) cout << node->data << " "; q.pop(); if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); nodeCount--; } isOdd = !isOdd; } } // Utility method to create a node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Driver code int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printOddNodes(root); return 0; }
Java
// Iterative Java program to print odd level nodes import java.util.*; class GfG { static class Node { int data; Node left, right; } // Iterative method to do level order traversal line by line static void printOddNodes(Node root) { // Base Case if (root == null) return; // Create an empty queue for level // order traversal Queue<Node> q = new LinkedList<Node> (); // Enqueue root and initialize level as odd q.add(root); boolean isOdd = true; while (true) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.size(); if (nodeCount == 0) break; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node node = q.peek(); if (isOdd == true) System.out.print(node.data + " "); q.remove(); if (node.left != null) q.add(node.left); if (node.right != null) q.add(node.right); nodeCount--; } isOdd = !isOdd; } } // Utility method to create a node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver code public static void main(String[] args) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); printOddNodes(root); } }
Python3
# Iterative Python3 program to print odd # level nodes # A Binary Tree Node # Utility function to create a # new tree Node class newNode: def __init__(self, data): self.data = data self.left = self.right = None # Iterative method to do level order # traversal line by line def printOddNodes(root) : # Base Case if (root == None): return # Create an empty queue for # level order traversal q = [] # Enqueue root and initialize # level as odd q.append(root) isOdd = True while (1) : # nodeCount (queue size) indicates # number of nodes at current level. nodeCount = len(q) if (nodeCount == 0) : break # Dequeue all nodes of current level # and Enqueue all nodes of next level while (nodeCount > 0): node = q[0] if (isOdd): print(node.data, end = " ") q.pop(0) if (node.left != None) : q.append(node.left) if (node.right != None) : q.append(node.right) nodeCount -= 1 isOdd = not isOdd # Driver Code if __name__ == '__main__': root = newNode(1) root.left = newNode(2) root.right = newNode(3) root.left.left = newNode(4) root.left.right = newNode(5) printOddNodes(root) # This code is contributed # by SHUBHAMSINGH10
C#
// Iterative C# program to // print odd level nodes using System; using System.Collections.Generic; public class GfG { public class Node { public int data; public Node left, right; } // Iterative method to do level // order traversal line by line static void printOddNodes(Node root) { // Base Case if (root == null) return; // Create an empty queue for level // order traversal Queue<Node> q = new Queue<Node> (); // Enqueue root and initialize level as odd q.Enqueue(root); bool isOdd = true; while (true) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.Count; if (nodeCount == 0) break; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node node = q.Peek(); if (isOdd == true) Console.Write(node.data + " "); q.Dequeue(); if (node.left != null) q.Enqueue(node.left); if (node.right != null) q.Enqueue(node.right); nodeCount--; } isOdd = !isOdd; } } // Utility method to create a node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver code public static void Main(String[] args) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); printOddNodes(root); } } // This code has been contributed // by 29AjayKumar
Javascript
<script> // Iterative Javascript program to print odd level nodes class Node { constructor(data) { this.data=data; this.left=this.right=null; } } function printOddNodes(root) { // Base Case if (root == null) return; // Create an empty queue for level // order traversal let q = []; // Enqueue root and initialize level as odd q.push(root); let isOdd = true; while (true) { // nodeCount (queue size) indicates // number of nodes at current level. let nodeCount = q.length; if (nodeCount == 0) break; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { let node = q[0]; if (isOdd == true) document.write(node.data + " "); q.shift(); if (node.left != null) q.push(node.left); if (node.right != null) q.push(node.right); nodeCount--; } isOdd = !isOdd; } } // Driver code let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); printOddNodes(root); // This code is contributed by rag2127 </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA