📌  相关文章
📜  计算二叉树中叶节点的迭代程序

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

计算二叉树中叶节点的迭代程序

给定一棵二叉树,在不使用递归的情况下计算树中的叶子。如果一个节点的左右子节点都为 NULL,则该节点是叶节点。

Example Tree

Leaves count for the above tree is 3.

这个想法是使用水平顺序遍历。在遍历过程中,如果我们找到一个左右子节点都为 NULL 的节点,我们就增加 count。

C++
// C++ program to count leaf nodes in a Binary Tree
#include 
using namespace std;
 
/* A binary tree Node has data, pointer to left
   child  and a pointer to right child */
struct Node
{
    int data;
    struct Node* left, *right;
};
 
/* Function to get the count of leaf Nodes in
   a binary tree*/
unsigned int getLeafCount(struct Node* node)
{
    // If tree is empty
    if (!node)
        return 0;
 
    // Initialize empty queue.
    queue q;
 
    // Do level order traversal starting from root
    int count = 0; // Initialize count of leaves
    q.push(node);
    while (!q.empty())
    {
        struct Node *temp = q.front();
        q.pop();
 
        if (temp->left != NULL)
            q.push(temp->left);
        if (temp->right != NULL)
            q.push(temp->right);
        if (temp->left == NULL && temp->right == NULL)
            count++;
    }
    return count;
}
 
/* Helper function that allocates a new Node with the
   given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
/* Driver program to test above functions*/
int main()
{
    /*   1
        /  \
      2     3
     / \
    4   5
    Let us create Binary Tree shown in
    above example */
 
    struct Node *root = newNode(1);
    root->left        = newNode(2);
    root->right       = newNode(3);
    root->left->left  = newNode(4);
    root->left->right = newNode(5);
 
    /* get leaf count of the above created tree */
    cout << getLeafCount(root);
 
    return 0;
}


Java
// Java program to count leaf nodes
// in a Binary Tree
import java.util.*;
 
class GFG
{
    /* A binary tree Node has data,
    pointer to left child and
    a pointer to right child */
    static class Node
    {
        int data;
        Node left, right;
    }
 
    /* Function to get the count of
    leaf Nodes in a binary tree*/
    static int getLeafCount(Node node)
    {
        // If tree is empty
        if (node == null)
        {
            return 0;
        }
 
        // Initialize empty queue.
        Queue q = new LinkedList<>();
 
        // Do level order traversal starting from root
        int count = 0; // Initialize count of leaves
        q.add(node);
        while (!q.isEmpty())
        {
            Node temp = q.peek();
            q.poll();
 
            if (temp.left != null)
            {
                q.add(temp.left);
            }
            if (temp.right != null)
            {
                q.add(temp.right);
            }
            if (temp.left == null &&
                temp.right == null)
            {
                count++;
            }
        }
        return count;
    }
 
    /* Helper function that allocates
    a new Node with the given data
    and null left and right pointers. */
    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)
    {
        {
            /*   1
                / \
               2   3
              / \
             4   5
            Let us create Binary Tree shown in
            above example */
            Node root = newNode(1);
            root.left = newNode(2);
            root.right = newNode(3);
            root.left.left = newNode(4);
            root.left.right = newNode(5);
 
            /* get leaf count of the above created tree */
            System.out.println(getLeafCount(root));
        }
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to count leaf nodes
# in a Binary Tree
from queue import Queue
 
# Helper function that allocates a new
# Node with the given data and None
# left and right pointers.
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
         
# Function to get the count of leaf
# Nodes in a binary tree
def getLeafCount(node):
     
    # If tree is empty
    if (not node):
        return 0
 
    # Initialize empty queue.
    q = Queue()
 
    # Do level order traversal
    # starting from root
    count = 0 # Initialize count of leaves
    q.put(node)
    while (not q.empty()):
        temp = q.queue[0]
        q.get()
 
        if (temp.left != None):
            q.put(temp.left)
        if (temp.right != None):
            q.put(temp.right)
        if (temp.left == None and
            temp.right == None):
            count += 1
    return count
 
# Driver Code
if __name__ == '__main__':
     
    # 1
    # / \
    # 2 3
    # / \
    # 4 5
    # Let us create Binary Tree shown
    # in above example
    root = newNode(1)
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.left = newNode(4)
    root.left.right = newNode(5)
 
    # get leaf count of the above
    # created tree
    print(getLeafCount(root))
 
# This code is contributed by PranchalK


C#
// C# program to count leaf nodes
// in a Binary Tree
using System;
using System.Collections.Generic;
     
class GFG
{
    /* A binary tree Node has data,
    pointer to left child and
    a pointer to right child */
    public class Node
    {
        public int data;
        public Node left, right;
    }
 
    /* Function to get the count of
    leaf Nodes in a binary tree*/
    static int getLeafCount(Node node)
    {
        // If tree is empty
        if (node == null)
        {
            return 0;
        }
 
        // Initialize empty queue.
        Queue q = new Queue();
 
        // Do level order traversal starting from root
        int count = 0; // Initialize count of leaves
        q.Enqueue(node);
        while (q.Count!=0)
        {
            Node temp = q.Peek();
            q.Dequeue();
 
            if (temp.left != null)
            {
                q.Enqueue(temp.left);
            }
            if (temp.right != null)
            {
                q.Enqueue(temp.right);
            }
            if (temp.left == null &&
                temp.right == null)
            {
                count++;
            }
        }
        return count;
    }
 
    /* Helper function that allocates
    a new Node with the given data
    and null left and right pointers. */
    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)
    {
        {
            /* 1
                / \
            2 3
            / \
            4 5
            Let us create Binary Tree shown in
            above example */
            Node root = newNode(1);
            root.left = newNode(2);
            root.right = newNode(3);
            root.left.left = newNode(4);
            root.left.right = newNode(5);
 
            /* get leaf count of the above created tree */
            Console.WriteLine(getLeafCount(root));
        }
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ Program for above approach
#include 
using namespace std;
 
// Node class
struct node
{
  int data;
  struct node* left;
  struct node* right;
};
 
// Program to count leaves
int countLeaves(struct node *node)
{
 
  // If the node itself is "null"
  // return 0, as there
  // are no leaves
  if(node == NULL)
  {
    return 0;
  }
 
  // It the node is a leaf then
  // both right and left
  // children will be "null"
  if(node->left == NULL && node->right == NULL)
  {
    return 1;
  }
 
  // Now we count the leaves in
  // the left and right
  // subtrees and return the sum
  return countLeaves(node->left) + countLeaves(node->right);
}
 
// Class newNode of Node type
struct node* newNode(int data)
{
  struct node* node =
    (struct node*)malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  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);
 
  /* get leaf count of the above
      created tree */
  cout<


Java
// Java Program for above approach
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
import java.util.*;
 
class GfG
{
   
  // Node class
  static class Node
  {
    int data;
    Node left, right;
  }
 
  // Program to count leaves
  static int countLeaves(Node node)
  {
 
    // If the node itself is "null"
    // return 0, as there
    // are no leaves
    if (node == null)
      return 0;
 
    // It the node is a leaf then
    // both right and left
    // children will be "null"
    if (node.left == null &&
                node.right == null)
      return 1;
 
    // Now we count the leaves in
    // the left and right
    // subtrees and return the sum
    return countLeaves(node.left)
             + countLeaves(node.right);
  }
  
  // Class newNode of Node type
  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);
 
      /* get leaf count of the above
      created tree */
      System.out.println(countLeaves(root));
  }
}
//Code by Rounik Prashar


Python3
# Python3 Program for above approach
 
# Node class
class Node:
    def __init__(self,x):
        self.data = x
        self.left = None
        self.right = None
 
# Program to count leaves
def countLeaves(node):
 
    # If the node itself is "None"
    # return 0, as there
    # are no leaves
    if (node == None):
        return 0
 
    # It the node is a leaf then
    # both right and left
    # children will be "None"
    if (node.left == None and node.right == None):
        return 1
 
    # Now we count the leaves in
    # the left and right
    # subtrees and return the sum
    return countLeaves(node.left) + countLeaves(node.right)
 
# Driver Code
if __name__ == '__main__':
 
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
 
      # /* get leaf count of the above
      # created tree */
    print(countLeaves(root))
 
    # This code is contributed by mohit kumar 29


C#
// C# Program for above approach
using System;
 
// Node class
public class Node
{
    public int data;
    public Node left, right;
   
    public Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
public class BinaryTree{
     
public static Node root;
 
// Program to count leaves
static int countLeaves(Node node)
{
     
    // If the node itself is "null"
    // return 0, as there
    // are no leaves
    if (node == null)
    {
        return 0;
    }
     
    // It the node is a leaf then
    // both right and left
    // children will be "null"
    if (node.left == null &&
        node.right == null)
    {
        return 1;
    }
     
    // Now we count the leaves in
    // the left and right
    // subtrees and return the sum
    return countLeaves(node.left) +
           countLeaves(node.right);
}
 
// Driver Code
static public void Main()
{
    BinaryTree.root = new Node(1);
    BinaryTree.root.left = new Node(2);
    BinaryTree.root.right = new Node(3);
    BinaryTree.root.left.left = new Node(4);
    BinaryTree.root.left.right = new Node(5);
     
    // Get leaf count of the above created tree
    Console.WriteLine(countLeaves(root));
}
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出
3

时间复杂度: O(n)

这是同一问题的递归解决方案:

这个想法很简单,我们将较大的树分解为较小的子树并求解它们以获得最终答案。

下面是上述方法的实现。

C++

// C++ Program for above approach
#include 
using namespace std;
 
// Node class
struct node
{
  int data;
  struct node* left;
  struct node* right;
};
 
// Program to count leaves
int countLeaves(struct node *node)
{
 
  // If the node itself is "null"
  // return 0, as there
  // are no leaves
  if(node == NULL)
  {
    return 0;
  }
 
  // It the node is a leaf then
  // both right and left
  // children will be "null"
  if(node->left == NULL && node->right == NULL)
  {
    return 1;
  }
 
  // Now we count the leaves in
  // the left and right
  // subtrees and return the sum
  return countLeaves(node->left) + countLeaves(node->right);
}
 
// Class newNode of Node type
struct node* newNode(int data)
{
  struct node* node =
    (struct node*)malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  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);
 
  /* get leaf count of the above
      created tree */
  cout<

Java

// Java Program for above approach
import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
import java.util.*;
 
class GfG
{
   
  // Node class
  static class Node
  {
    int data;
    Node left, right;
  }
 
  // Program to count leaves
  static int countLeaves(Node node)
  {
 
    // If the node itself is "null"
    // return 0, as there
    // are no leaves
    if (node == null)
      return 0;
 
    // It the node is a leaf then
    // both right and left
    // children will be "null"
    if (node.left == null &&
                node.right == null)
      return 1;
 
    // Now we count the leaves in
    // the left and right
    // subtrees and return the sum
    return countLeaves(node.left)
             + countLeaves(node.right);
  }
  
  // Class newNode of Node type
  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);
 
      /* get leaf count of the above
      created tree */
      System.out.println(countLeaves(root));
  }
}
//Code by Rounik Prashar

Python3

# Python3 Program for above approach
 
# Node class
class Node:
    def __init__(self,x):
        self.data = x
        self.left = None
        self.right = None
 
# Program to count leaves
def countLeaves(node):
 
    # If the node itself is "None"
    # return 0, as there
    # are no leaves
    if (node == None):
        return 0
 
    # It the node is a leaf then
    # both right and left
    # children will be "None"
    if (node.left == None and node.right == None):
        return 1
 
    # Now we count the leaves in
    # the left and right
    # subtrees and return the sum
    return countLeaves(node.left) + countLeaves(node.right)
 
# Driver Code
if __name__ == '__main__':
 
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
 
      # /* get leaf count of the above
      # created tree */
    print(countLeaves(root))
 
    # This code is contributed by mohit kumar 29

C#

// C# Program for above approach
using System;
 
// Node class
public class Node
{
    public int data;
    public Node left, right;
   
    public Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
public class BinaryTree{
     
public static Node root;
 
// Program to count leaves
static int countLeaves(Node node)
{
     
    // If the node itself is "null"
    // return 0, as there
    // are no leaves
    if (node == null)
    {
        return 0;
    }
     
    // It the node is a leaf then
    // both right and left
    // children will be "null"
    if (node.left == null &&
        node.right == null)
    {
        return 1;
    }
     
    // Now we count the leaves in
    // the left and right
    // subtrees and return the sum
    return countLeaves(node.left) +
           countLeaves(node.right);
}
 
// Driver Code
static public void Main()
{
    BinaryTree.root = new Node(1);
    BinaryTree.root.left = new Node(2);
    BinaryTree.root.right = new Node(3);
    BinaryTree.root.left.left = new Node(4);
    BinaryTree.root.left.right = new Node(5);
     
    // Get leaf count of the above created tree
    Console.WriteLine(countLeaves(root));
}
}
 
// This code is contributed by avanitrachhadiya2155

Javascript


输出
3

https://youtu.be/N2mV5p8NOVw?list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk