📌  相关文章
📜  获取二叉树中节点的级别

📅  最后修改于: 2022-05-13 01:57:16.976000             🧑  作者: Mango

获取二叉树中节点的级别

给定一个二叉树和一个键,编写一个返回键级别的函数。
例如,考虑下面的树。如果输入键是 3,那么你的函数应该返回 1。如果输入键是 4,那么你的函数应该返回 3。对于键中不存在的键,那么你的函数应该返回 0。

思路是从root开始,level为1。如果key与root的数据匹配,则返回level。否则递归调用级别为级别 + 1 的左右子树。

C++
// C++ program to Get Level of a
// node in a Binary Tree
#include
using namespace std;
 
/* A tree node structure */
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
 
/* Helper function for getLevel().
It returns level of the data if data is
present in tree, otherwise returns 0.*/
int getLevelUtil(struct node *node,
                 int data, int level)
{
    if (node == NULL)
        return 0;
 
    if (node -> data == data)
        return level;
 
    int downlevel = getLevelUtil(node -> left,
                                 data, level + 1);
    if (downlevel != 0)
        return downlevel;
 
    downlevel = getLevelUtil(node->right,
                             data, level + 1);
    return downlevel;
}
 
/* Returns level of given data value */
int getLevel(struct node *node, int data)
{
    return getLevelUtil(node, data, 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 = NULL;
    temp -> right = NULL;
 
    return temp;
}
 
// Driver Code
int main()
{
    struct node *root = new struct node;
    int x;
 
    /* 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);
 
    for (x = 1; x <= 5; x++)
    {
        int level = getLevel(root, x);
        if (level)
            cout << "Level of "<< x << " is "
                 << getLevel(root, x) << endl;
        else
            cout << x << "is not present in tree"
                      << endl;
    }
 
    getchar();
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// C program to Get Level of a node in a Binary Tree
#include
#include
 
/* A tree node structure */
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
 
/* Helper function for getLevel(). 
    It returns level of the data if data is
   present in tree, otherwise returns 0.*/
int getLevelUtil(struct node *node,
                 int data, int level)
{
    if (node == NULL)
        return 0;
 
    if (node->data == data)
        return level;
 
    int downlevel = getLevelUtil(node->left,
                                 data, level+1);
    if (downlevel != 0)
        return downlevel;
 
    downlevel = getLevelUtil(node->right,
                             data, level+1);
    return downlevel;
}
 
/* Returns level of given data value */
int getLevel(struct node *node, int data)
{
    return getLevelUtil(node,data,1);
}
 
/* Utility function to create
   a new Binary Tree node */
struct node* newNode(int data)
{
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
 
    return temp;
}
 
/* Driver code */
int main()
{
    struct node *root;
    int x;
 
    /* 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);
 
    for (x = 1; x <=5; x++)
    {
      int level = getLevel(root, x);
      if (level)
        printf(" Level of %d is %d\n", x, getLevel(root, x));
      else
        printf(" %d is not present in tree \n", x);
 
    }
 
    getchar();
    return 0;
}


Java
// Java program to Get Level of a node in a Binary Tree
/* A tree node structure */
class Node {
    int data;
    Node left, right;
 
    public Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
class BinaryTree {
 
    Node root;
 
    /* Helper function for getLevel().
       It returns level of
       the data if data is present in tree,
       otherwise returns
       0.*/
    int getLevelUtil(Node node, int data, int level)
    {
        if (node == null)
            return 0;
 
        if (node.data == data)
            return level;
 
        int downlevel
            = getLevelUtil(node.left, data, level + 1);
        if (downlevel != 0)
            return downlevel;
 
        downlevel
            = getLevelUtil(node.right, data, level + 1);
        return downlevel;
    }
 
    /* Returns level of given data value */
    int getLevel(Node node, int data)
    {
        return getLevelUtil(node, data, 1);
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructing tree given in the above figure */
        tree.root = new Node(3);
        tree.root.left = new Node(2);
        tree.root.right = new Node(5);
        tree.root.left.left = new Node(1);
        tree.root.left.right = new Node(4);
        for (int x = 1; x <= 5; x++)
        {
            int level = tree.getLevel(tree.root, x);
            if (level != 0)
                System.out.println(
                    "Level of " + x + " is "
                    + tree.getLevel(tree.root, x));
            else
                System.out.println(
                    x + " is not present in tree");
        }
    }
}
 
// This code has been contributed by Mayank
// Jaiswal(mayank_24)


Python3
# Python3 program to Get Level of a
# node in a Binary Tree
 
# Helper function that allocates a
# new node with the given data and
# None left and right pairs.
 
 
class newNode:
 
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Helper function for getLevel(). It
# returns level of the data if data is
# present in tree, otherwise returns 0
 
 
def getLevelUtil(node, data, level):
    if (node == None):
        return 0
 
    if (node.data == data):
        return level
 
    downlevel = getLevelUtil(node.left,
                             data, level + 1)
    if (downlevel != 0):
        return downlevel
 
    downlevel = getLevelUtil(node.right,
                             data, level + 1)
    return downlevel
 
# Returns level of given data value
 
 
def getLevel(node, data):
 
    return getLevelUtil(node, data, 1)
 
 
# Driver Code
if __name__ == '__main__':
 
    # Let us construct the Tree shown
    # 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)
    for x in range(1, 6):
        level = getLevel(root, x)
        if (level):
            print("Level of", x,
                  "is", getLevel(root, x))
        else:
            print(x, "is not present in tree")
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# program to Get Level of a node in a Binary Tree
using System;
 
/* A tree node structure */
public class Node {
    public int data;
    public Node left, right;
 
    public Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
public class BinaryTree {
 
    public Node root;
 
    /* Helper function for getLevel().
       It returns level of
       the data if data is present in tree,
      otherwise returns
    0.*/
    public virtual int getLevelUtil(Node node, int data,
                                    int level)
    {
        if (node == null)
        {
            return 0;
        }
 
        if (node.data == data)
        {
            return level;
        }
 
        int downlevel
            = getLevelUtil(node.left, data, level + 1);
        if (downlevel != 0)
        {
            return downlevel;
        }
 
        downlevel
            = getLevelUtil(node.right, data, level + 1);
        return downlevel;
    }
 
    /* Returns level of given data value */
    public virtual int getLevel(Node node, int data)
    {
        return getLevelUtil(node, data, 1);
    }
 
    /* Driver code */
    public static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructing tree given in the above figure */
        tree.root = new Node(3);
        tree.root.left = new Node(2);
        tree.root.right = new Node(5);
        tree.root.left.left = new Node(1);
        tree.root.left.right = new Node(4);
        for (int x = 1; x <= 5; x++)
        {
            int level = tree.getLevel(tree.root, x);
            if (level != 0)
            {
                Console.WriteLine(
                    "Level of " + x + " is "
                    + tree.getLevel(tree.root, x));
            }
            else
            {
                Console.WriteLine(
                    x + " is not present in tree");
            }
        }
    }
}
 
// This code is contributed by Shrikant13


Javascript


C++
/* C++ program to print level in which X is present
        in binary tree using STL */
#include 
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
int printLevel(Node* root, int X)
{
    Node* node;
    // Base Case
    if (root == NULL)
        return 0;
 
    // Create an empty queue for level order traversal
    queue q;
 
    // Create a var represent current level of tree
    int currLevel = 1;
 
    // Enqueue Root and initialize height
    q.push(root);
 
    while (q.empty() == false) {
        // Print front of queue and remove it from queue
 
        int size = q.size();
 
        // traversing level by level
        while (size--) {
 
            node = q.front();
 
            if (node->data == X) {
               return currLevel;
                
            }
 
            q.pop();
 
            /* Enqueue left child */
            if (node->left != NULL)
                q.push(node->left);
 
            /*Enqueue right child */
            if (node->right != NULL)
                q.push(node->right);
        }
         
 
        // increment the level
        currLevel++;
    }
  // if element not found
  return 0;
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in above diagram
    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(7);
    root->right->right = newNode(6);
 
     
    cout<


输出
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

getLevel() 的时间复杂度为 O(n),其中 n 是给定二叉树中的节点数。

替代方法:给定问题可以借助给定二叉树的级别顺序遍历来解决。

C++

/* C++ program to print level in which X is present
        in binary tree using STL */
#include 
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
int printLevel(Node* root, int X)
{
    Node* node;
    // Base Case
    if (root == NULL)
        return 0;
 
    // Create an empty queue for level order traversal
    queue q;
 
    // Create a var represent current level of tree
    int currLevel = 1;
 
    // Enqueue Root and initialize height
    q.push(root);
 
    while (q.empty() == false) {
        // Print front of queue and remove it from queue
 
        int size = q.size();
 
        // traversing level by level
        while (size--) {
 
            node = q.front();
 
            if (node->data == X) {
               return currLevel;
                
            }
 
            q.pop();
 
            /* Enqueue left child */
            if (node->left != NULL)
                q.push(node->left);
 
            /*Enqueue right child */
            if (node->right != NULL)
                q.push(node->right);
        }
         
 
        // increment the level
        currLevel++;
    }
  // if element not found
  return 0;
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in above diagram
    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(7);
    root->right->right = newNode(6);
 
     
    cout<

输出:3

时间复杂度:O(n)其中 n 是二叉树中的节点数

辅助空间:O(n) ,其中 n 是二叉树中的节点数