📜  打印位于叶节点上方的节点

📅  最后修改于: 2021-06-25 18:30:32             🧑  作者: Mango

给定一个由N个节点组成的二叉树,任务是打印在叶节点上方的节点。
例子:

方法:想法是遍历树,并针对每个节点检查它是否可以是位于叶节点上方的树。为此,当前节点必须具有子节点,并且至少一个子节点应该是叶节点。步骤如下:

  • 遍历树并检查每个节点。
  • 如果当前节点有两个子节点,请检查其中是否有一个是根节点。如果是,则打印当前节点。
  • 如果当前节点只有左或右子节点,则检查该左或右子节点是否是叶节点。如果是,则打印当前节点。
  • 否则,继续遍历树并移至下一个节点。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Node of tree
struct node {
    int data;
    struct node *left, *right;
};
 
// Creates and initializes a new
// node for the tree
struct node* newnode(int data)
{
    struct node* temp = new node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Prints all nodes which are just
// above leaf node
void cal(struct node* root)
{
 
    // If tree is empty
    if (root == NULL) {
        return;
    }
 
    // If it is a leaf node
    if (root->left == NULL
        && root->right == NULL) {
        return;
    }
 
    // For internal nodes
    else {
 
        // If node has two children
        if (root->left != NULL
            && root->right != NULL) {
 
            if ((root->left->left == NULL
                 && root->left->right == NULL)
                || (root->right->left == NULL
                    && root->right->right == NULL)) {
 
                cout << root->data << " ";
            }
        }
 
        // If node has only left child
        if (root->left != NULL
            && root->right == NULL) {
 
            if (root->left->left == NULL
                && root->left->right == NULL) {
 
                cout << root->data << " ";
            }
        }
 
        // If node has only right child
        if (root->right != NULL
            && root->left == NULL) {
 
            if (root->right->left == NULL
                && root->right->right == NULL) {
 
                cout << root->data << " ";
            }
        }
    }
 
    // Recursively Call for left
    // and right subtree
    cal(root->left);
    cal(root->right);
}
 
// Driver Code
int main()
{
    // Construct a tree
    node* root = newnode(20);
    root->left = newnode(8);
    root->right = newnode(22);
    root->left->left = newnode(4);
    root->left->right = newnode(12);
    root->left->right->left = newnode(10);
    root->left->right->right = newnode(14);
 
    // Function Call
    cal(root);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
// Class containing the left and right
// child of current node and the
// key value
class Node
{
    int data;
    Node left, right;
 
    // Constructor of the class
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
 
class GFG{
     
Node root;
 
// Prints all nodes which are just
// above leaf node
static void cal(Node root)
{
 
    // If tree is empty
    if (root == null)
    {
        return;
    }
 
    // If it is a leaf node
    if (root.left == null &&
       root.right == null)
    {
        return;
    }
 
    // For internal nodes
    else
    {
 
        // If node has two children
        if (root.left != null &&
           root.right != null)
        {
            if ((root.left.left == null &&
                root.left.right == null) ||
               (root.right.left == null &&
               root.right.right == null))
            {
                System.out.print(root.data + " ");
            }
        }
 
        // If node has only left child
        if (root.left != null &&
           root.right == null)
        {
            if (root.left.left == null &&
               root.left.right == null)
            {
                System.out.print(root.data + " ");
            }
        }
 
        // If node has only right child
        if (root.right != null &&
             root.left == null)
        {
            if (root.right.left == null &&
               root.right.right == null)
            {
                System.out.print(root.data + " ");
            }
        }
    }
 
    // Recursively call for left
    // and right subtree
    cal(root.left);
    cal(root.right);
}
     
// Driver Code
public static void main (String[] args)
{
    GFG tree = new GFG();
 
    tree.root = new Node(20);
    tree.root.left = new Node(8);
    tree.root.right = new Node(22);
 
    tree.root.left.left = new Node(4);
    tree.root.left.right = new Node(12);
 
    tree.root.left.right.left = new Node(10);
    tree.root.left.right.right = new Node(14);
     
    // Function call
    cal(tree.root);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the
# above approach
 
# Node of tree
class newNode:
   
    def __init__(self, data):
       
        self.data = data
        self.left = None
        self.right = None
 
# Creates and initializes a new
# node for the tree
 
# Prints all nodes which are
# just above leaf node
def cal(root):
   
    # If tree is empty
    if (root == None):
        return
 
    # If it is a leaf node
    if (root.left == None and
        root.right == None):
        return
 
    # For internal nodes
    else:
       
        # If node has two children
        if (root.left != None and
            root.right != None):
            if ((root.left.left == None and
                 root.left.right == None) or
                (root.right.left == None and
                 root.right.right == None)):
                print(root.data, end = " ")
 
        # If node has only left child
        if (root.left != None and
            root.right == None):
            if (root.left.left == None and
                root.left.right == None):
                print(root.data, end = " ")
 
        # If node has only right child
        if (root.right != None and
            root.left == None):
            if (root.right.left == None and
                root.right.right == None):
                print(root.data, end = " ")
 
    # Recursively Call for left
    # and right subtree
    cal(root.left)
    cal(root.right)
 
# Driver Code
if __name__ == '__main__':
   
    # Construct a tree
    root = newNode(20)
    root.left = newNode(8)
    root.right = newNode(22)
    root.left.left = newNode(4)
    root.left.right = newNode(12)
    root.left.right.left = newNode(10)
    root.left.right.right = newNode(14)
 
    # Function Call
    cal(root)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
// Class containing the left and right
// child of current node and the
// key value
class Node
{
    public int data;
    public Node left, right;
 
    // Constructor of the class
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
 
class GFG{   
Node root;
 
// Prints all nodes which are just
// above leaf node
static void cal(Node root)
{
    // If tree is empty
    if (root == null)
    {
        return;
    }
 
    // If it is a leaf node
    if (root.left == null &&
        root.right == null)
    {
        return;
    }
 
    // For internal nodes
    else
    {
        // If node has two children
        if (root.left != null &&
            root.right != null)
        {
            if ((root.left.left == null &&
                 root.left.right == null) ||
                (root.right.left == null &&
                 root.right.right == null))
            {
                Console.Write(root.data + " ");
            }
        }
 
        // If node has only left child
        if (root.left != null &&
            root.right == null)
        {
            if (root.left.left == null &&
                root.left.right == null)
            {
                Console.Write(root.data + " ");
            }
        }
 
        // If node has only right child
        if (root.right != null &&
            root.left == null)
        {
            if (root.right.left == null &&
                root.right.right == null)
            {
                Console.Write(root.data + " ");
            }
        }
    }
 
    // Recursively call for left
    // and right subtree
    cal(root.left);
    cal(root.right);
}
     
// Driver Code
public static void Main(String[] args)
{
    GFG tree = new GFG();
    tree.root = new Node(20);
    tree.root.left = new Node(8);
    tree.root.right = new Node(22);
    tree.root.left.left = new Node(4);
    tree.root.left.right = new Node(12);
    tree.root.left.right.left = new Node(10);
    tree.root.left.right.right = new Node(14);
     
    // Function call
    cal(tree.root);
}
}
 
// This code is contributed by Rajput-Ji


输出:
20 8 12





时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。