📌  相关文章
📜  在二叉树中计算从根到自身的路径中具有最高值的节点

📅  最后修改于: 2021-04-29 02:59:04             🧑  作者: Mango

对于给定的二叉树,任务是计算二叉树中的节点数,这些节点是从根到该节点的路径中价值最高的节点。

例子:

方法:想法是对给定的二叉树执行有序遍历,并在每次递归调用之后更新到目前为止在路径中获得的最大值节点。请按照以下步骤操作:

  • 在给定的二叉树上执行有序遍历
  • 在每次递归调用之后,更新从根节点到当前节点的路径中到目前为止遇到的最大价值的节点。
  • 如果节点的值超出了路径中到目前为止的最大值节点,则将计数增加1并更新到目前为止获得的最大值。
  • 继续到当前节点的子树。
  • 遍历二叉树后,打印获得的计数

下面是上述方法的实现:

C++14
// C++14 program for the above approach
#include 
using namespace std;
 
// Stores the ct of
// nodes which are maximum
// in the path from root
// to the current node
int ct = 0;
 
// Binary Tree Node
struct Node
{
    int val;
    Node *left, *right;
     
    Node(int x)
    {
        val = x;
        left = right = NULL;
    }
};
 
// Function that performs Inorder
// Traversal on the Binary Tree
void find(Node *root, int mx)
{
     
    // If root does not exist
    if (root == NULL)
      return;
     
    // Check if the node
    // satisfies the condition
    if (root->val >= mx)
        ct++;
     
    // Update the maximum value
    // and recursively traverse
    // left and right subtree
    find(root->left, max(mx, root->val));
     
    find(root->right, max(mx, root->val));
}
 
// Function that counts the good
// nodes in the given Binary Tree
int NodesMaxInPath(Node* root)
{
     
    // Perform inorder Traversal
    find(root, INT_MIN);
     
    // Return the final count
    return ct;
}
 
// Driver code
int main()
{
     
    /* A Binary Tree
              3
             / \
            2   5
           /     \
          4       6
        */
    Node* root = new Node(3);
    root->left = new Node(2);
    root->right = new Node(5);
    root->left->left = new Node(4);
    root->right->right = new Node(7);
     
    // Function call
    int answer = NodesMaxInPath(root);
     
    // Print the count of good nodes
    cout << (answer);
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
import java.util.*;
 
class GfG {
 
    // Stores the count of
    // nodes which are maximum
    // in the path from root
    // to the current node
    static int count = 0;
 
    // Binary Tree Node
    static class Node {
        int val;
        Node left, right;
    }
 
    // Function that performs Inorder
    // Traversal on the Binary Tree
    static void find(Node root, int max)
    {
        // If root does not exist
        if (root == null)
            return;
 
        // Check if the node
        // satisfies the condition
        if (root.val >= max)
            count++;
 
        // Update the maximum value
        // and recursively traverse
        // left and right subtree
        find(root.left,
             Math.max(max, root.val));
 
        find(root.right,
             Math.max(max, root.val));
    }
 
    // Function that counts the good
    // nodes in the given Binary Tree
    static int NodesMaxInPath(Node root)
    {
        // Perform inorder Traversal
        find(root, Integer.MIN_VALUE);
 
        // Return the final count
        return count;
    }
 
    // Function that add the new node
    // in the Binary Tree
    static Node newNode(int data)
    {
        Node temp = new Node();
        temp.val = data;
        temp.left = null;
        temp.right = null;
 
        // Return the node
        return temp;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        /* A Binary Tree
              3
             / \
            2   5
           /     \
          4       6
        */
 
        Node root = null;
        root = newNode(3);
        root.left = newNode(2);
        root.right = newNode(5);
        root.left.left = newNode(4);
        root.right.right = newNode(7);
 
        // Function Call
        int answer = NodesMaxInPath(root);
 
        // Print the count of good nodes
        System.out.println(answer);
    }
}


Python3
# Python 3 program for the
# above approach
import sys
 
# Stores the ct of
# nodes which are maximum
# in the path from root
# to the current node
ct = 0
 
# Binary Tree Node
class newNode:
   
    def __init__(self, x):
       
        self.val = x
        self.left = None
        self.right = None
 
# Function that performs Inorder
# Traversal on the Binary Tree
def find(root, mx):
   
    global ct
     
    # If root does not exist
    if (root == None):
        return
     
    # Check if the node
    # satisfies the condition
    if (root.val >= mx):
        ct += 1
     
    # Update the maximum value
    # and recursively traverse
    # left and right subtree
    find(root.left,
         max(mx, root.val))
     
    find(root.right,
         max(mx, root.val))
 
# Function that counts
# the good nodes in the
# given Binary Tree
def NodesMaxInPath(root):
   
    global ct
     
    # Perform inorder
    # Traversal
    find(root,
         -sys.maxsize-1)
     
    # Return the final count
    return ct
 
# Driver code
if __name__ == '__main__':
   
    '''
    /* A Binary Tree
              3
             / /
            2   5
           /     /
          4       6
        */
    '''
    root = newNode(3)
    root.left = newNode(2)
    root.right = newNode(5)
    root.left.left = newNode(4)
    root.right.right = newNode(7)
     
    # Function call
    answer = NodesMaxInPath(root)
     
    # Print the count of good nodes
    print(answer)
 
# This code is contributed by Surendra_Gangwar


C#
// C# program for
// the above approach
using System;
class GfG{
 
// Stores the count of
// nodes which are maximum
// in the path from root
// to the current node
static int count = 0;
 
// Binary Tree Node
public class Node
{
  public int val;
  public Node left,
              right;
}
 
// Function that performs
// Inorder Traversal on
// the Binary Tree
static void find(Node root,
                 int max)
{
  // If root does not exist
  if (root == null)
    return;
 
  // Check if the node
  // satisfies the condition
  if (root.val >= max)
    count++;
 
  // Update the maximum value
  // and recursively traverse
  // left and right subtree
  find(root.left,
  Math.Max(max, root.val));
 
  find(root.right,
  Math.Max(max, root.val));
}
 
    // Function that counts the good
    // nodes in the given Binary Tree
    static int NodesMaxInPath(Node root)
    {
        // Perform inorder Traversal
        find(root, int.MinValue);
 
        // Return the readonly count
        return count;
    }
 
// Function that add the new node
// in the Binary Tree
static Node newNode(int data)
{
  Node temp = new Node();
  temp.val = data;
  temp.left = null;
  temp.right = null;
 
  // Return the node
  return temp;
}
 
// Driver Code
public static void Main(String[] args)
{
  /* A Binary Tree
              3
             / \
            2   5
           /     \
          4       6
        */
 
  Node root = null;
  root = newNode(3);
  root.left = newNode(2);
  root.right = newNode(5);
  root.left.left = newNode(4);
  root.right.right = newNode(7);
 
  // Function Call
  int answer = NodesMaxInPath(root);
 
  // Print the count of good nodes
  Console.WriteLine(answer);
}
}
 
// This code is contributed by Princi Singh


输出:
4








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