📌  相关文章
📜  按排序顺序打印给定两个二叉树的元素

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

按排序顺序打印给定两个二叉树的元素

给定两棵二叉树,任务是以非递减顺序打印两棵二叉树的元素。

例子:

方法:给定的问题可以使用以下步骤来解决:

  • 创建一个地图以存储两棵树中存在的每个元素以及频率。
  • 遍历第一棵树并在地图中插入每个元素及其频率。
  • 类似地,遍历第一棵树并在图中插入每个元素及其频率。
  • 遍历地图并打印所有元素及其频率时间。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Structure of a binary tree node
class node {
public:
    int data;
    node* left;
    node* right;
};
 
// Helper function that allocates
// a new node with the given data
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
 
    return (Node);
}
 
// Map to store all elements
// from given two trees
map m;
 
// Recursive function to perform
// inorder traversal on tree
void traverse(node* root)
{
    // Base Case
    if (root == NULL)
        return;
    else {
        // Update map
        m[root->data]++;
    }
 
    // Recursive call for left subtree
    traverse(root->left);
 
    // Recursive call for right subtree
    traverse(root->right);
}
 
// Function to print all the elements of
// two given binary trees in sorted order
void printAllElements(node* root1, node* root2)
{
    // Traverse the 1st tree
    traverse(root1);
 
    // Traverse the 2nd tree
    traverse(root2);
 
    // Traverse the map
    for (auto it : m) {
        // Print current element
        // its frequency times
        for (int i = 0; i < it.second; i++) {
            cout << it.first << " ";
        }
    }
}
 
// Driver code
int main()
{
    node* root1 = newNode(8);
    root1->left = newNode(2);
    root1->right = newNode(10);
    root1->left->left = newNode(1);
 
    node* root2 = newNode(5);
    root2->left = newNode(3);
    root2->right = newNode(0);
 
    printAllElements(root1, root2);
 
    return 0;
}


Python3
# Python code for the above approach
 
# Structure of a binary tree node
class node:
    def __init__(self, d):
        self.data = d
        self.left = None
        self.right = None
 
# Map to store all elements
# from given two trees
m = {}
 
# Recursive function to perform
# inorder traversal on tree
def traverse(root):
 
    # Base Case
    if (root == None):
        return
    else:
 
        # Update map
        if (root.data in m):
            m[root.data] += 1
        else:
            m[root.data] = 1
 
    # Recursive call for left subtree
    traverse(root.left)
 
    # Recursive call for right subtree
    traverse(root.right)
 
# Function to print all the elements of
# two given binary trees in sorted order
def printAllElements(root1, root2):
 
    # Traverse the 1st tree
    traverse(root1)
 
    # Traverse the 2nd tree
    traverse(root2)
    v = []
 
    # Traverse the map
    for key in m:
 
        # Print current element
        # its frequency times
        for i in range(m[key]):
            v.append(key)
 
    v.sort()
    for i in range(len(v)):
        print(v[i], end=" ")
 
# Driver code
root1 = node(8)
root1.left = node(2)
root1.right = node(10)
root1.left.left = node(1)
 
root2 = node(5)
root2.left = node(3)
root2.right = node(0)
 
printAllElements(root1, root2)
 
# This code is contributed by Saurabh Jaiswal


Javascript



输出
0 1 2 3 5 8 10 

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