原文:
给定一棵二叉树,任务是在树的级别顺序遍历中打印偶数级别的奇数位置节点。根被认为是级别 0 ,任何级别的最左边的节点被认为是位置 0 的节点。 例:
input:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
/ \
10 11
output: 5 7 11
input:
2
/ \
4 15
/ /
45 17
output: 17
先决条件– 方法:要逐层打印节点,请使用层级顺序遍历。思路基于。为此,逐级遍历节点,并在每一级后切换奇数级标志。同样,将每一级中的第二个节点标记为奇数位置,并在每次处理下一个节点后切换它。 以下是上述方法的实施:
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 printevenleveloddnodes(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->left->left = newnode(10);
root->left->right->right->right = newnode(11);
printevenleveloddnodes(root);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the above 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 printevenleveloddnodes(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.left.left = newnode(10);
root.left.right.right.right = newnode(11);
printevenleveloddnodes(root);
}
}
// this code is contributed by princi singh
python 3
# python 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 printevenleveloddnodes(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 not 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.left.left = newnode(10)
root.left.right.right.right = newnode(11)
printevenleveloddnodes(root)
c
// c# implementation of the above 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 printevenleveloddnodes(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.left.left = newnode(10);
root.left.right.right.right = newnode(11);
printevenleveloddnodes(root);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
5 7 11
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处