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

📅  最后修改于: 2021-04-29 05:21:47             🧑  作者: Mango

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

例子:

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

  • 以对角线顺序遍历给定的二叉树,并将每个对角线的起始节点存储为,对于每个,将所有对角线中的值存储在Hashset中。
  • 遍历后,找到具有等于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)