原文:

给定一棵二叉树,在水平顺序遍历中打印偶数层的偶数定位节点。根在级别 0 处考虑,任意级别最左边的节点在位置 0 处考虑为节点。 例:

input:
         1
       /   \
      2     3
    /   \     \
   4     5      6
        /  \
       7    8
      /      \
     9        10
output: 1 4 6 9
input:
      2
    /   \
   4     15
  /     /
 45   17
output: 2 45

方法:要逐级打印节点,请使用级别顺序遍历。思路是基于。为此,逐级遍历节点,并在每一级后切换偶数级标志。同样,将每一级中的第一个节点标记为偶数位置,并在每次处理下一个节点后切换它。 以下是上述方法的实施:

c

// c   implementation of the approach
#include 
using namespace std;
struct node {
    int data;
    node *left, *right;
};
// iterative method to do level order
// traversal line by line
void printevenlevelevennodes(node* root)
{
    // base case
    if (root == null)
        return;
    // create an empty queue for level
    // order traversal
    queue q;
    // enqueue root and initialize level as even
    q.push(root);
    bool evenlevel = true;
    while (1) {
        // nodecount (queue size) indicates
        // number of nodes in the current level
        int nodecount = q.size();
        if (nodecount == 0)
            break;
        // mark 1st node as even positioned
        bool evennodeposition = true;
        // dequeue all the nodes of current level
        // and enqueue all the nodes of next level
        while (nodecount > 0) {
            node* node = q.front();
            // print only even positioned
            // nodes of even levels
            if (evenlevel && evennodeposition)
                cout << node->data << " ";
            q.pop();
            if (node->left != null)
                q.push(node->left);
            if (node->right != null)
                q.push(node->right);
            nodecount--;
            // switch the even position flag
            evennodeposition = !evennodeposition;
        }
        // switch the even level flag
        evenlevel = !evenlevel;
    }
}
// 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);
    root->right->left = newnode(6);
    root->right->right = newnode(7);
    root->left->right->left = newnode(8);
    root->left->right->right = newnode(9);
    root->left->right->right->right = newnode(10);
    printevenlevelevennodes(root);
    return 0;
}

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

// java implementation of the approach
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 printevenlevelevennodes(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 even
    q.add(root);
    boolean evenlevel = true;
    while (true)
    {
        // nodecount (queue size) indicates
        // number of nodes in the current level
        int nodecount = q.size();
        if (nodecount == 0)
            break;
        // mark 1st node as even positioned
        boolean evennodeposition = true;
        // dequeue all the nodes of current level
        // and enqueue all the nodes of next level
        while (nodecount > 0)
        {
            node node = q.peek();
            // print only even positioned
            // nodes of even levels
            if (evenlevel && evennodeposition)
                system.out.print(node.data   " ");
            q.remove();
            if (node.left != null)
                q.add(node.left);
            if (node.right != null)
                q.add(node.right);
            nodecount--;
            // switch the even position flag
            evennodeposition = !evennodeposition;
        }
        // switch the even level flag
        evenlevel = !evenlevel;
    }
}
// utility method to create a node
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = 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);
    root.right.left = newnode(6);
    root.right.right = newnode(7);
    root.left.right.left = newnode(8);
    root.left.right.right = newnode(9);
    root.left.right.right.right = newnode(10);
    printevenlevelevennodes(root);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 implementation of the approach
# utility method to create a node
class newnode:
    # construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = none
        self.right = none
# iterative method to do level order
# traversal line by line
def printevenlevelevennodes(root):
    # base case
    if (root == none):
        return
    # create an empty queue for level
    # order traversal
    q = []
    # enqueue root and initialize
    # level as even
    q.append(root)
    evenlevel = true
    while (1):
        # nodecount (queue size) indicates
        # number of nodes in the current level
        nodecount = len(q)
        if (nodecount == 0):
            break
        # mark 1st node as even positioned
        evennodeposition = true
        # dequeue all the nodes of current level
        # and enqueue all the nodes of next level
        while (nodecount > 0):
            node = q[0]
            # pronly even positioned
            # nodes of even levels
            if (evenlevel and evennodeposition):
                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
            # switch the even position flag
            evennodeposition = not evennodeposition
        # switch the even level flag
        evenlevel = not evenlevel
# 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)
    root.right.left = newnode(6)
    root.right.right = newnode(7)
    root.left.right.left = newnode(8)
    root.left.right.right = newnode(9)
    root.left.right.right.right = newnode(10)
    printevenlevelevennodes(root)
# this code is contributed by shubhamsingh10

c

// c# implementation of the approach
using system;
using system.collections.generic;
class gfg
{
public class node
{
    public int data;
    public node left, right;
};
// iterative method to do level order
// traversal line by line
static void printevenlevelevennodes(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 even
    q.enqueue(root);
    bool evenlevel = true;
    while (true)
    {
        // nodecount (queue size) indicates
        // number of nodes in the current level
        int nodecount = q.count;
        if (nodecount == 0)
            break;
        // mark 1st node as even positioned
        bool evennodeposition = true;
        // dequeue all the nodes of current level
        // and enqueue all the nodes of next level
        while (nodecount > 0)
        {
            node node = q.peek();
            // print only even positioned
            // nodes of even levels
            if (evenlevel && evennodeposition)
                console.write(node.data   " ");
            q.dequeue();
            if (node.left != null)
                q.enqueue(node.left);
            if (node.right != null)
                q.enqueue(node.right);
            nodecount--;
            // switch the even position flag
            evennodeposition = !evennodeposition;
        }
        // switch the even level flag
        evenlevel = !evenlevel;
    }
}
// utility method to create a node
static node newnode(int data)
{
    node node = new node();
    node.data = data;
    node.left = 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);
    root.right.left = newnode(6);
    root.right.right = newnode(7);
    root.left.right.left = newnode(8);
    root.left.right.right = newnode(9);
    root.left.right.right.right = newnode(10);
    printevenlevelevennodes(root);
}
}
// this code is contributed by princiraj1992

java 描述语言


output: 

1 4 6 10