原文:
给定一个二叉树和一个键,编写一个函数,打印给定二叉树中所有键的级别。 例如,考虑下面的树。如果输入键是 3,那么你的函数应该返回 1。如果输入键是 4,那么你的函数应该返回 3。对于键中不存在的键,您的函数应该返回 0。
input:
3
/ \
2 5
/ \
1 4
output:
level of 1 is 3
level of 2 is 2
level of 3 is 1
level of 4 is 3
level of 5 is 2
我们已经在下面的帖子中讨论了递归pg电子试玩链接的解决方案。 本文讨论了一种基于的迭代求解方法。遍历时,我们将队列中每个节点的级别与该节点存储在一起。
c
// an iterative c program to print levels
// of all nodes
#include
using namespace std;
/* a tree node structure */
struct node {
int data;
struct node* left;
struct node* right;
};
void printlevel(struct node* root)
{
if (!root)
return;
// queue to hold tree node with level
queue > q;
q.push({root, 1}); // let root node be at level 1
pair p;
// do level order traversal of tree
while (!q.empty()) {
p = q.front();
q.pop();
cout << "level of " << p.first->data
<< " is " << p.second << "\n";
if (p.first->left)
q.push({ p.first->left, p.second 1 });
if (p.first->right)
q.push({ p.first->right, p.second 1 });
}
}
/* utility function to create a new binary tree node */
struct node* newnode(int data)
{
struct node* temp = new struct node;
temp->data = data;
temp->left = temp->right = null;
return temp;
}
/* driver function to test above functions */
int main()
{
struct node* root = null;
/* constructing tree given in the above figure */
root = newnode(3);
root->left = newnode(2);
root->right = newnode(5);
root->left->left = newnode(1);
root->left->right = newnode(4);
printlevel(root);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print
// levels of all nodes
import java.util.linkedlist;
import java.util.queue;
public class print_level_btree {
/* a tree node structure */
static class node {
int data;
node left;
node right;
node(int data){
this.data = data;
left = null;
right = null;
}
}
// user defined class pair to hold
// the node and its level
static class pair{
node n;
int i;
pair(node n, int i){
this.n = n;
this.i = i;
}
}
// function to print the nodes and
// its corresponding level
static void printlevel(node root)
{
if (root == null)
return;
// queue to hold tree node with level
queue q = new linkedlist();
// let root node be at level 1
q.add(new pair(root, 1));
pair p;
// do level order traversal of tree
while (!q.isempty()) {
p = q.peek();
q.remove();
system.out.println("level of " p.n.data
" is " p.i);
if (p.n.left != null)
q.add(new pair(p.n.left, p.i 1));
if (p.n.right != null)
q.add(new pair(p.n.right, p.i 1));
}
}
/* driver function to test above
functions */
public static void main(string args[])
{
node root = null;
/* constructing tree given in the
above figure */
root = new node(3);
root.left = new node(2);
root.right = new node(5);
root.left.left = new node(1);
root.left.right = new node(4);
printlevel(root);
}
}
// this code is contributed by sumit ghosh
python 3
# python3 program to print levels
# of all nodes
# helper function that allocates a new
# node with the given data and none
# left and right poers.
class newnode:
# construct to create a new node
def __init__(self, key):
self.data = key
self.left = none
self.right = none
def prlevel( root):
if (not root):
return
# queue to hold tree node with level
q = []
# let root node be at level 1
q.append([root, 1])
p = []
# do level order traversal of tree
while (len(q)):
p = q[0]
q.pop(0)
print("level of", p[0].data, "is", p[1])
if (p[0].left):
q.append([p[0].left, p[1] 1])
if (p[0].right):
q.append([p[0].right, p[1] 1 ])
# driver code
if __name__ == '__main__':
"""
let us create binary tree shown
in above example """
root = newnode(3)
root.left = newnode(2)
root.right = newnode(5)
root.left.left = newnode(1)
root.left.right = newnode(4)
prlevel(root)
# this code is contributed by
# shubham singh(shubhamsingh10)
c
using system;
using system.collections.generic;
// c# program to print
// levels of all nodes
public class print_level_btree
{
/* a tree node structure */
public class node
{
public int data;
public node left;
public node right;
public node(int data)
{
this.data = data;
left = null;
right = null;
}
}
// user defined class pair to hold
// the node and its level
public class pair
{
public node n;
public int i;
public pair(node n, int i)
{
this.n = n;
this.i = i;
}
}
// function to print the nodes and
// its corresponding level
public static void printlevel(node root)
{
if (root == null)
{
return;
}
// queue to hold tree node with level
linkedlist q = new linkedlist();
// let root node be at level 1
q.addlast(new pair(root, 1));
pair p;
// do level order traversal of tree
while (q.count > 0)
{
p = q.first.value;
q.removefirst();
console.writeline("level of " p.n.data " is " p.i);
if (p.n.left != null)
{
q.addlast(new pair(p.n.left, p.i 1));
}
if (p.n.right != null)
{
q.addlast(new pair(p.n.right, p.i 1));
}
}
}
/* driver function to test above
functions */
public static void main(string[] args)
{
node root = null;
/* constructing tree given in the
above figure */
root = new node(3);
root.left = new node(2);
root.right = new node(5);
root.left.left = new node(1);
root.left.right = new node(4);
printlevel(root);
}
}
// this code is contributed by shrikant13
java 描述语言
输出:
level of 3 is 1
level of 2 is 2
level of 5 is 2
level of 1 is 3
level of 4 is 3
时间复杂度: o(n),其中 n 是给定二叉树中的节点数。
本文由 abhishek rajput 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处