> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/trees/binary-trees-custom-tree.md).

# Binary Trees Custom Tree

You are given 2 Strings of equal length.

1. Let the strings be str1 and str2. Now, we have have a link between str1.charAt(i)

   and str2.charAt(i) such that str1.charAt(i) is the parent and str2.charAt(i) is the child.
2. Keeping this relation in mind, you have to form a binary tree and print the contents.

**Sample Input**&#x20;

```
aabcdczyx
bgcdefyxw
```

**Sample Output**&#x20;

```
b <= a => g
c <= b => .
d <= c => f
e <= d => .
. <= e => .
. <= f => .
. <= g => .

y <= z => .
x <= y => .
w <= x => .
. <= w => .
```

```java
class Solution {
    public void populateHelper(String s1, String s2, HashMap<Character, Node> map, HashMap<Character, Boolean> marker,
            int index) {
        if (index == s1.length())
            return;
        Node root = map.getOrDefault(s1.charAt(index), new Node(s1.charAt(index)));
        if (!marker.containsKey(s1.charAt(index)))
            marker.put(s1.charAt(index), true);
        Node child = map.getOrDefault(s2.charAt(index), new Node(s2.charAt(index)));
        marker.put(s2.charAt(index), false);
        if (root.left == null)
            root.left = child;
        else
            root.right = child;
        map.put(s1.charAt(index), root);
        map.put(s2.charAt(index), child);

        populateHelper(s1, s2, map, marker, index + 1);
    }

    public ArrayList<Node> populate(String str1, String str2) {
        HashMap<Character, Node> map = new HashMap<>();
        HashMap<Character, Boolean> marker = new HashMap<>();
        populateHelper(str1, str2, map, marker, 0);
        ArrayList<Node> ans = new ArrayList<>();
        for (char x : marker.keySet())
            if (marker.get(x))
                ans.add(map.get(x));
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/binary-trees-custom-tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
