原文:

给定一个二叉树,任务是从左到右遍历给定二叉树的每一级,并打印在某一级遇到的每个备选项。

示例:

输入:

输出:t2 1 2 3 9 5 7

输入:

输出:t2】71 88 4 6 8 10 13

方法:对给定的二叉树进行遍历即可解决问题。按照以下步骤解决问题:

  1. 初始化一个,存储给定二叉树每一级的节点。
  2. 执行并打印二叉树每一级的交替节点

下面是上述方法的实现:

c

// c   program to implement
// the above approach
#include 
using namespace std;
// structure of a node
struct node {
    int data;
    node* left;
    node* right;
    node(int val)
    {
        data = val;
        left = right = null;
    }
};
// print alternate nodes of
// a binary tree
void printalternate(node* root)
{
    // store nodes of each level
    queue q;
    q.push(root);
    while (!q.empty()) {
        // store count of nodes
        // of current level
        int n = q.size();
        // print alternate nodes of
        // the current level
        for (int i = 0; i < n; i  ) {
            node* temp = q.front();
            q.pop();
            if (i % 2 == 0) {
                cout << temp->data << " ";
            }
            // if left child exists
            if (temp->left) {
                // store left child
                q.push(temp->left);
            }
            // if right child exists
            if (temp->right) {
                // store right child
                q.push(temp->right);
            }
        }
        cout << endl;
    }
}
// driver code
int main()
{
    node* root;
    // create a tree
    root = new node(71);
    root->left = new node(88);
    root->right = new node(99);
    root->left->left = new node(4);
    root->left->right = new node(5);
    root->right->left = new node(6);
    root->right->right = new node(7);
    root->left->left->left = new node(8);
    root->left->left->right = new node(9);
    root->left->right->left = new node(10);
    root->left->right->right = new node(11);
    root->right->left->right = new node(13);
    root->right->right->left = new node(14);
    printalternate(root);
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement
// the above approach
import java.util.*;
class gfg{
// structure of a node
static class node
{
  int data;
  node left;
  node right;
  node(int val)
  {
    data = val;
    left = right = null;
  }
};
// print alternate nodes of
// a binary tree
static void printalternate(node root)
{
  // store nodes of each level
  queue q = new linkedlist<>();
  q.add(root);
  while (!q.isempty())
  {
    // store count of nodes
    // of current level
    int n = q.size();
    // print alternate nodes of
    // the current level
    for (int i = 0; i < n; i  )
    {
      node temp = q.peek();
      q.remove();
      if (i % 2 == 0)
      {
        system.out.print(temp.data   " ");
      }
      // if left child exists
      if (temp.left!=null)
      {
        // store left child
        q.add(temp.left);
      }
      // if right child exists
      if (temp.right!=null)
      {
        // store right child
        q.add(temp.right);
      }
    }
    system.out.println();
  }
}
// driver code
public static void main(string[] args)
{
  node root;
  // create a tree
  root = new node(71);
  root.left = new node(88);
  root.right = new node(99);
  root.left.left = new node(4);
  root.left.right = new node(5);
  root.right.left = new node(6);
  root.right.right = new node(7);
  root.left.left.left = new node(8);
  root.left.left.right = new node(9);
  root.left.right.left = new node(10);
  root.left.right.right = new node(11);
  root.right.left.right = new node(13);
  root.right.right.left = new node(14);
  printalternate(root);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 program to implement
# the above approach
# structure of a node
class newnode:
    def __init__(self, val):
        self.data = val
        self.left = none
        self.right = none
# print alternate nodes of
# a binary tree
def printalternate(root):
    # store nodes of each level
    q = []
    q.append(root)
    while (len(q)):
        # store count of nodes
        # of current level
        n = len(q)
        # print alternate nodes of
        # the current level
        for i in range(n):
            temp = q[0]
            q.remove(q[0])
            if (i % 2 == 0):
                print(temp.data, end = " ")
            # if left child exists
            if (temp.left):
                # store left child
                q.append(temp.left)
            # if right child exists
            if (temp.right):
                # store right child
                q.append(temp.right)
        print("\n", end = "")
# driver code
if __name__ == '__main__':
    # create a tree
    root = newnode(71)
    root.left = newnode(88)
    root.right = newnode(99)
    root.left.left = newnode(4)
    root.left.right = newnode(5)
    root.right.left = newnode(6)
    root.right.right = newnode(7)
    root.left.left.left = newnode(8)
    root.left.left.right = newnode(9)
    root.left.right.left = newnode(10)
    root.left.right.right = newnode(11)
    root.right.left.right = newnode(13)
    root.right.right.left = newnode(14)
    printalternate(root)
# this code is contributed by ipg2016107

c

// c# program to implement
// the above approach
using system;
using system.collections.generic;
class gfg{
// structure of a node
class node
{
  public int data;
  public node left;
  public node right;
  public node(int val)
  {
    data = val;
    left = right = null;
  }
};
// print alternate nodes of
// a binary tree
static void printalternate(node root)
{
  // store nodes of each level
  queue q = new queue();
  q.enqueue(root);
  while (q.count != 0)
  {
    // store count of nodes
    // of current level
    int n = q.count;
    // print alternate nodes of
    // the current level
    for (int i = 0; i < n; i  )
    {
      node temp = q.peek();
      q.dequeue();
      if (i % 2 == 0)
      {
        console.write(temp.data   " ");
      }
      // if left child exists
      if (temp.left!=null)
      {
        // store left child
        q.enqueue(temp.left);
      }
      // if right child exists
      if (temp.right!=null)
      {
        // store right child
        q.enqueue(temp.right);
      }
    }
    console.writeline();
  }
}
// driver code
public static void main(string[] args)
{
  node root;
  // create a tree
  root = new node(71);
  root.left = new node(88);
  root.right = new node(99);
  root.left.left = new node(4);
  root.left.right = new node(5);
  root.right.left = new node(6);
  root.right.right = new node(7);
  root.left.left.left = new node(8);
  root.left.left.right = new node(9);
  root.left.right.left = new node(10);
  root.left.right.right = new node(11);
  root.right.left.right = new node(13);
  root.right.right.left = new node(14);
  printalternate(root);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

71 
88 
4 6 
8 10 13

时间复杂度:o(n) t5辅助空间:** o(n)