📌  相关文章
📜  打印树的奇数级别的节点

📅  最后修改于: 2022-05-13 01:57:20.932000             🧑  作者: Mango

打印树的奇数级别的节点

给定一棵二叉树,以任意顺序打印奇数级节点。根被认为是第 1 级。

For example consider the following tree
          1
       /     \
      2       3
    /   \       \
   4     5       6
        /  \     /
       7    8   9

Output  1 4 5 6

方法1(递归)

这个想法是在每个递归调用中将初始级别作为奇数和切换级别标志传递。对于每个节点,如果设置了奇数标志,则打印它。

C++
// Recursive C++ program to print odd level nodes
#include 
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


C++
// Iterative C++ program to print odd level nodes
#include 
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 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 q = new LinkedList ();
 
    // 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 q = new Queue ();
 
        // 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


输出:

1 4 5

时间复杂度: O(n)
辅助空间:在倾斜树的情况下,递归堆栈空间为 O(n)

方法2(迭代)

上面的代码以预购方式打印节点。如果我们希望逐级打印节点,我们可以使用级别顺序遍历。这个想法是基于逐行遍历打印级别的顺序
我们逐级遍历节点。我们在每个级别后切换奇数级别标志。

C++

// Iterative C++ program to print odd level nodes
#include 
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 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 q = new LinkedList ();
 
    // 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 q = new Queue ();
 
        // 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


输出:

1 4 5

时间复杂度: O(n)
辅助空间:队列数据结构的 O(n)