原文:
给定一个二叉树,打印级别顺序遍历,所有级别的节点都打印在单独的行中。 例如,考虑下面的树
example 1:
output for above tree should be
20
8 22
4 12
10 14
example 2:
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
output for above tree should be
1
2 3
4 5 6
7 8 9<
注意,这与不同,这里我们需要一起打印所有节点。这里我们需要在不同的行中打印不同级别的节点。 一个简单的pg电子试玩链接的解决方案是使用中讨论的递归函数进行打印,并在每次调用 后打印一个新行。
c
/* function to line by line print level order traversal a tree*/
void printlevelorder(struct node* root)
{
int h = height(root);
int i;
for (i=1; i<=h; i )
{
printgivenlevel(root, i);
printf("\n");
}
}
/* print nodes at a given level */
void printgivenlevel(struct node* root, int level)
{
if (root == null)
return;
if (level == 1)
printf("%d ", root->data);
else if (level > 1)
{
printgivenlevel(root->left, level-1);
printgivenlevel(root->right, level-1);
}
}
java 语言(一种计算机语言,尤用于创建网站)
/* function to line by line print level order traversal a tree*/
static void printlevelorder(node root)
{
int h = height(root);
int i;
for (i=1; i<=h; i )
{
printgivenlevel(root, i);
system.out.println();
}
}
/* print nodes at a given level */
void printgivenlevel(node root, int level)
{
if (root == null)
return;
if (level == 1)
system.out.println(root.data);
else if (level > 1)
{
printgivenlevel(root.left, level-1);
printgivenlevel(root.right, level-1);
}
}
python 3
# python3 program for above approach
def printlevelorder(root):
h = height(root)
for i in range(1, h 1):
givenspirallevel(root, i)
def printgivenlevel(root, level):
if root is none:
return root
if level == 1:
print(root.val, end = ' ')
elif level > 1:
printgivenlevel(root.left, level - 1)
printgivenlevel(root.right, level - 1)
# this code is contributed by praveen kumar
c
/* print nodes at a given level */
static void printgivenlevel(node root, int level)
{
if (root == null)
return;
if (level == 1)
console.writeline(root.data);
else if (level > 1)
{
printgivenlevel(root.left, level-1);
printgivenlevel(root.right, level-1);
}
}
java 描述语言
/* print nodes at a given level */
function printgivenlevel(root, level)
{
if (root == null)
return;
if (level == 1)
document.write(root.data);
else if (level > 1)
{
printgivenlevel(root.left, level-1);
printgivenlevel(root.right, level-1);
}
}
以上解的时间复杂度为 o(n 2 ) 如何将迭代的关卡顺序遍历(方法二 )逐行修改到关卡? 的思路类似于的岗位。我们计算当前级别的节点数。对于每个节点,我们将其子节点排队。
c
/* iterative program to print levels line by line */
#include
#include
using namespace std;
// a binary tree node
struct node
{
struct node *left;
int data;
struct node *right;
};
// iterative method to do level order traversal
// line by line
void printlevelorder(node *root)
{
// base case
if (root == null) return;
// create an empty queue for level order traversal
queue q;
// enqueue root and initialize height
q.push(root);
while (q.empty() == false)
{
// nodecount (queue size) indicates number
// of nodes at current level.
int nodecount = q.size();
// dequeue all nodes of current level and
// enqueue all nodes of next level
while (nodecount > 0)
{
node *node = q.front();
cout << node->data << " ";
q.pop();
if (node->left != null)
q.push(node->left);
if (node->right != null)
q.push(node->right);
nodecount--;
}
cout << endl;
}
}
// utility function to create a new tree node
node* newnode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = null;
temp->right = null;
return temp;
}
// driver program to test above functions
int main()
{
// let us create binary tree shown above
node *root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->right->right = newnode(6);
printlevelorder(root);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
/* an iterative java program to print levels line by line */
import java.util.linkedlist;
import java.util.queue;
public class levelorder
{
// a binary tree node
static class node
{
int data;
node left;
node right;
// constructor
node(int data){
this.data = data;
left = null;
right =null;
}
}
// iterative method to do level order traversal line by line
static void printlevelorder(node root)
{
// base case
if(root == null)
return;
// create an empty queue for level order traversal
queue q =new linkedlist();
// enqueue root and initialize height
q.add(root);
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();
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 program to test above functions
public static void main(string[] args)
{
// let us create binary tree shown in above diagram
/* 1
/ \
2 3
/ \ \
4 5 6
*/
node root = new node(1);
root.left = new node(2);
root.right = new node(3);
root.left.left = new node(4);
root.left.right = new node(5);
root.right.right = new node(6);
printlevelorder(root);
}
}
//this code is contributed by sumit ghosh
python 3
# python3 program for above approach
class newnode:
def __init__(self, data):
self.val = data
self.left = none
self.right = none
# iterative method to do level order traversal
# line by line
def printlevelorder(root):
# base case
if root is none:
return
# create an empty queue for level order traversal
q = []
# enqueue root and initialize height
q.append(root)
while q:
# nodecount (queue size) indicates number
# of nodes at current level.
count = len(q)
# dequeue all nodes of current level and
# enqueue all nodes of next level
while count > 0:
temp = q.pop(0)
print(temp.val, end = ' ')
if temp.left:
q.append(temp.left)
if temp.right:
q.append(temp.right)
count -= 1
print(' ')
# driver code
root = newnode(1);
root.left = newnode(2);
root.right = newnode(3);
root.left.left = newnode(4);
root.left.right = newnode(5);
root.right.right = newnode(6);
printlevelorder(root);
# this code is contributed by praveen kumar
c
/* an iterative c# program to print
levels line by line */
using system;
using system.collections.generic;
public class levelorder
{
// a binary tree node
class node
{
public int data;
public node left;
public node right;
// constructor
public node(int data)
{
this.data = data;
left = null;
right =null;
}
}
// iterative method to do level order
// traversal line by line
static void printlevelorder(node root)
{
// base case
if(root == null)
return;
// create an empty queue for level
// order traversal
queue q =new queue();
// enqueue root and initialize height
q.enqueue(root);
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();
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)
{
// let us create binary tree shown
// in above diagram
/* 1
/ \
2 3
/ \ \
4 5 6
*/
node root = new node(1);
root.left = new node(2);
root.right = new node(3);
root.left.left = new node(4);
root.left.right = new node(5);
root.right.right = new node(6);
printlevelorder(root);
}
}
// this code is contributed 29ajaykumar
java 描述语言
输出:
1
2 3
4 5 6
该方法的时间复杂度为 o(n),其中 n 是给定二叉树中的节点数。 如发现有不正确的地方,请写评论,或者想分享更多以上讨论话题的信息
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处