📜  获取二叉树中的最大左节点

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

获取二叉树中的最大左节点

给定一棵树,任务是在二叉树的唯一左节点中找到最大值。
例子:

Input : 
           7
         /    \
        6       5
       / \     / \
      4  3     2  1 
Output :
6

Input :
            1
         /    \
        2       3
       /       / \
      4       5   6
        \    /  \ 
         7  8   9 
Output :
8

用中序遍历并仅对左节点应用条件并获得左节点的最大值。
让我们尝试用代码来理解。

C++
// CPP program to print maximum element
// in left node.
#include 
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
// Get max of left element using
// Inorder traversal
int maxOfLeftElement(Node* root)
{
    int res = INT_MIN;
    if (root == NULL)
        return res;
 
    if (root->left != NULL)
        res = root->left->data;
 
    // Return maximum of three values
    // 1) Recursive max in left subtree
    // 2) Value in left node
    // 3) Recursive max in right subtree
    return max({ maxOfLeftElement(root->left),
                 res,
                 maxOfLeftElement(root->right) });
}
 
// 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(7);
    root->left = newNode(6);
    root->right = newNode(5);
    root->left->left = newNode(4);
    root->left->right = newNode(3);
    root->right->left = newNode(2);
    root->right->right = newNode(1);
 
    /*     7
         /    \
        6       5
       / \     / \
      4  3     2  1          */
    cout << maxOfLeftElement(root);
    return 0;
}


Java
// Java program to print maximum element
// in left node.
import java.util.*;
class GfG {
 
// A Binary Tree Node
static class Node {
    int data;
    Node left, right;
}
 
// Get max of left element using
// Inorder traversal
static int maxOfLeftElement(Node root)
{
    int res = Integer.MIN_VALUE;
    if (root == null)
        return res;
 
    if (root.left != null)
        res = root.left.data;
 
    // Return maximum of three values
    // 1) Recursive max in left subtree
    // 2) Value in left node
    // 3) Recursive max in right subtree
    return Math.max(maxOfLeftElement(root.left),
       Math.max(res, maxOfLeftElement(root.right)));
}
 
// Utility function to create a new tree node
static 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
public static void main(String[] args)
{
    // Let us create binary tree shown in above diagram
    Node root = newNode(7);
    root.left = newNode(6);
    root.right = newNode(5);
    root.left.left = newNode(4);
    root.left.right = newNode(3);
    root.right.left = newNode(2);
    root.right.right = newNode(1);
 
    /* 7
        / \
        6 5
    / \ / \
    4 3 2 1         */
    System.out.println(maxOfLeftElement(root));
}
}


Python3
# Python program to print maximum element
# in left node.
 
# Utility class to create a
# new tree node
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None
     
# Get max of left element using
# Inorder traversal
def maxOfLeftElement(root):
    res = -999999999999
    if (root == None):
        return res
 
    if (root.left != None):
        res = root.left.data
 
    # Return maximum of three values
    # 1) Recursive max in left subtree
    # 2) Value in left node
    # 3) Recursive max in right subtree
    return max({ maxOfLeftElement(root.left), res,
                 maxOfLeftElement(root.right) })
 
# Driver Code
if __name__ == '__main__':
 
    # Let us create binary tree shown
    # in above diagram
    root = newNode(7)
    root.left = newNode(6)
    root.right = newNode(5)
    root.left.left = newNode(4)
    root.left.right = newNode(3)
    root.right.left = newNode(2)
    root.right.right = newNode(1)
 
    #     7
    #     / \
    # 6     5
    # / \     / \
    # 4 3     2 1        
    print(maxOfLeftElement(root))
 
# This code is contributed by PranchalK


C#
// C# program to print maximum element
// in left node.
using System;
 
class GfG
{
 
    // A Binary Tree Node
    class Node
    {
        public int data;
        public Node left, right;
    }
 
    // Get max of left element using
    // Inorder traversal
    static int maxOfLeftElement(Node root)
    {
        int res = int.MinValue;
        if (root == null)
            return res;
 
        if (root.left != null)
            res = root.left.data;
 
        // Return maximum of three values
        // 1) Recursive max in left subtree
        // 2) Value in left node
        // 3) Recursive max in right subtree
        return Math.Max(maxOfLeftElement(root.left),
        Math.Max(res, maxOfLeftElement(root.right)));
    }
 
    // Utility function to create a new tree node
    static Node newNode(int data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.left = null;
        temp.right = null;
        return temp;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Let us create binary tree
        // shown in above diagram
        Node root = newNode(7);
        root.left = newNode(6);
        root.right = newNode(5);
        root.left.left = newNode(4);
        root.left.right = newNode(3);
        root.right.left = newNode(2);
        root.right.right = newNode(1);
 
        /* 7
            / \
            6 5
        / \ / \
        4 3 2 1         */
        Console.WriteLine(maxOfLeftElement(root));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
6