原文:
给定一棵二叉树,任务是打印除了树的每一级中最左边的节点之外的所有节点。根被视为 0 级,任何级别的最左侧节点被视为 0 位置的节点。
示例:
input:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
/ \
9 10
output:
3
5 6
8
10
input:
1
/ \
2 3
\ \
4 5
output:
3
5
方法:要逐级打印节点,请使用级别顺序遍历。思路是基于。为此,逐级遍历节点,并在每级处理之前标记最左边的标志为真,在每级处理第一个节点之后标记为假。
下面是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
// structure of the tree node
struct node {
int data;
node *left, *right;
};
// 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);
}
// function to print all the nodes
// except the leftmost in every level
// of the given binary tree
// with level order traversal
void excludeleftmost(node* root)
{
// base case
if (root == null)
return;
// create an empty queue for level
// order traversal
queue q;
// enqueue root
q.push(root);
while (1) {
// nodecount (queue size) indicates
// number of nodes at current level.
int nodecount = q.size();
if (nodecount == 0)
break;
// initialize leftmost as true
// just before the beginning
// of each level
bool leftmost = true;
// dequeue all nodes of current level
// and enqueue all nodes of next level
while (nodecount > 0) {
node* node = q.front();
// switch leftmost flag after processing
// the leftmost node
if (leftmost)
leftmost = !leftmost;
// print all the nodes except leftmost
else
cout << node->data << " ";
q.pop();
if (node->left != null)
q.push(node->left);
if (node->right != null)
q.push(node->right);
nodecount--;
}
cout << "\n";
}
}
// 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);
excludeleftmost(root);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
import java.util.*;
class sol
{
// structure of the tree node
static class node
{
int data;
node left, right;
};
// 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);
}
// function to print all the nodes
// except the leftmost in every level
// of the given binary tree
// with level order traversal
static void excludeleftmost(node root)
{
// base case
if (root == null)
return;
// create an empty queue for level
// order traversal
queue q = new linkedlist();
// enqueue root
q.add(root);
while (true)
{
// nodecount (queue size) indicates
// number of nodes at current level.
int nodecount = q.size();
if (nodecount == 0)
break;
// initialize leftmost as true
// just before the beginning
// of each level
boolean leftmost = true;
// dequeue all nodes of current level
// and enqueue all nodes of next level
while (nodecount > 0)
{
node node = q.peek();
// switch leftmost flag after processing
// the leftmost node
if (leftmost)
leftmost = !leftmost;
// print all the nodes except leftmost
else
system.out.print( node.data " ");
q.remove();
if (node.left != null)
q.add(node.left);
if (node.right != null)
q.add(node.right);
nodecount--;
}
system.out.println();
}
}
// 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);
excludeleftmost(root);
}
}
// this code is contributed by arnab kundu
python 3
# python implementation of the approach
from collections import deque
# structure of the tree node
class node:
def __init__(self):
self.data = 0
self.left = none
self.right = none
# utility method to create a node
def newnode(data: int) -> node:
node = node()
node.data = data
node.left = none
node.right = none
return node
# function to print all the nodes
# except the leftmost in every level
# of the given binary tree
# with level order traversal
def excludeleftmost(root: node):
# base case
if root is none:
return
# create an empty queue for level
# order traversal
q = deque()
# enqueue root
q.append(root)
while 1:
# nodecount (queue size) indicates
# number of nodes at current level
nodecount = len(q)
if nodecount == 0:
break
# initialize leftmost as true
# just before the beginning
# of each level
leftmost = true
# dequeue all nodes of current level
# and enqueue all nodes of next level
while nodecount > 0:
node = q[0]
# switch leftmost flag after processing
# the leftmost node
if leftmost:
leftmost = not leftmost
# print all the nodes except leftmost
else:
print(node.data, end=" ")
q.popleft()
if node.left is not none:
q.append(node.left)
if node.right is not none:
q.append(node.right)
nodecount -= 1
print()
# driver code
if __name__ == "__main__":
root = 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)
excludeleftmost(root)
# this code is contributed by
# sanjeev2552
c
// c# implementation of the above approach
using system;
using system.collections.generic;
class gfg
{
// structure of the tree node
public class node
{
public int data;
public node left, right;
};
// 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);
}
// function to print all the nodes
// except the leftmost in every level
// of the given binary tree
// with level order traversal
static void excludeleftmost(node root)
{
// base case
if (root == null)
return;
// create an empty queue for level
// order traversal
queue q = new queue();
// enqueue root
q.enqueue(root);
while (true)
{
// nodecount (queue size) indicates
// number of nodes at current level.
int nodecount = q.count;
if (nodecount == 0)
break;
// initialize leftmost as true
// just before the beginning
// of each level
boolean leftmost = true;
// dequeue all nodes of current level
// and enqueue all nodes of next level
while (nodecount > 0)
{
node node = q.peek();
// switch leftmost flag after processing
// the leftmost node
if (leftmost)
leftmost = !leftmost;
// print all the nodes except leftmost
else
console.write( node.data " ");
q.dequeue();
if (node.left != null)
q.enqueue(node.left);
if (node.right != null)
q.enqueue(node.right);
nodecount--;
}
console.writeline();
}
}
// 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);
excludeleftmost(root);
}
}
// this code is contributed by princiraj1992
java 描述语言
output:
3
5 6 7
9
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处