📌  相关文章
📜  计算从节点到由相同值节点组成的叶子的对角路径

📅  最后修改于: 2021-09-04 08:37:11             🧑  作者: Mango

给定一棵二叉树,任务是找到到二叉树叶子的对角路径数,使得同一对角线上所有节点的值相等。

例子:

方法:主要思想是使用 Map 对角遍历树。请按照以下步骤解决此问题:

  • 以对角线顺序遍历给定的二叉树,并将每个对角线的起始节点存储为,对于每个,将该对角线上的所有值存储在一个哈希集中。
  • 遍历后,找到集合大小等于1数并将其打印为答案。

下面是上述方法的实现。

C++14
5
      / \
     6   5
      \   \
       6   5


Java
5
      / \
     6   5
      \   \
       5   5


Python3
// C++ program of the above approach
#include 
using namespace std;
 
struct TreeNode
{
    int val = 0;
    TreeNode *left, *right;
     
    TreeNode(int x)
    {
        val = x;
        left = NULL;
        right = NULL;
    }
};
 
// Function to perform diagonal
// traversal on the given binary tree
void fillMap(TreeNode *root, int left,
             map> &diag)
{
     
    // If tree is empty
    if (!root)
        return;
 
    // If current diagonal is not visited
    if (diag[left].size() == 0)
    {
         
        // Update diag[left]
        diag[left].insert(root->val);
    }
     
    // Otherwise, map current node
    // with its diagonal
    else
        diag[left].insert(root->val);
 
    // Recursively, traverse left subtree
    fillMap(root->left, left + 1, diag);
 
    // Recursively, traverse right subtree
    fillMap(root->right, left, diag);
}
 
// Function to count diagonal
// paths having same-valued nodes
int sameDiag(TreeNode *root)
{
     
    // Maps the values of all
    // nodes with its diagonal
    map> diag;
 
    // Stores indexing of diagonal
    int left = 0;
 
    // Function call to perform
    // diagonal traversal
    fillMap(root, left, diag);
 
    // Stores count of diagonals such
    // that all the nodes on the same
    // diagonal are equal
    int count = 0;
 
    // Traverse each diagonal
    for(auto d : diag)
    {
         
        // If all nodes on the current
        // diagonal are equal
        if (diag[d.first].size() == 1)
         
            // Update count
            count += 1;
    }
    return count;
}
 
// Driver Code
int main()
{
     
    // Given tree
    TreeNode *root = new TreeNode(5);
    root->left = new TreeNode(6);
    root->right = new TreeNode(5);
    root->left->right = new TreeNode(6);
    root->right->right = new TreeNode(5);
     
    // Function call
    cout << sameDiag(root);
}
 
// This code is contributed by mohit kumar 29


输出:
// Java program for above approach
import java.util.*;
import java.lang.*;
class GFG
{
 
  // Structure of a Node
  static class TreeNode
  {
    int val;
    TreeNode left, right;
 
    TreeNode(int key)
    {
      val = key;
      left = null;
      right = null;
    }
  };
 
  // Function to perform diagonal
  // traversal on the given binary tree
  static void fillMap(TreeNode root, int left,
                      Map> diag)
  {
 
    // If tree is empty
    if (root == null)
      return;
 
    // If current diagonal is not visited
    if (diag.get(left) == null)
    {
 
      // Update diag[left]
      diag.put(left, new HashSet());
      diag.get(left).add(root.val);
    }
 
    // Otherwise, map current node
    // with its diagonal
    else
      diag.get(left).add(root.val);
 
    // Recursively, traverse left subtree
    fillMap(root.left, left + 1, diag);
 
    // Recursively, traverse right subtree
    fillMap(root.right, left, diag);
  }
 
  // Function to count diagonal
  // paths having same-valued nodes
  static int sameDiag(TreeNode root)
  {
 
    // Maps the values of all
    // nodes with its diagonal
    Map> diag = new HashMap<>();
 
    // Stores indexing of diagonal
    int left = 0;
 
    // Function call to perform
    // diagonal traversal
    fillMap(root, left, diag);
 
    // Stores count of diagonals such
    // that all the nodes on the same
    // diagonal are equal
    int count = 0;
 
    // Traverse each diagonal
    for(Map.Entry> d:diag.entrySet())
    {
 
      // If all nodes on the current
      // diagonal are equal
      if (d.getValue().size() == 1)
 
        // Update count
        count += 1;
    }
    return count;
  }
 
  // Driver function
  public static void main (String[] args)
  {
    TreeNode root = new TreeNode(5);
    root.left = new TreeNode(6);
    root.right = new TreeNode(5);
    root.left.right = new TreeNode(6);
    root.right.right = new TreeNode(5);
 
    System.out.println(sameDiag(root));
  }
}
// This code is contributed by offbeat

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live