> 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/print-common-nodes-on-path-from-root-or-common-ancestors.md).

# Print common nodes on path from root (or common ancestors)

Given a binary tree and two nodes, the task is to Print all the nodes that are common for 2 given nodes in a binary tree.

**Examples:**

```
Given binary tree is :
                   1
                 /    \
                2      3
              /   \   / \
             4     5 6   7
            /       / \
           8       9   10

Given nodes 9 and 7, so the common nodes are:-
1, 3
```

```java
class Solution {
    boolean foundP = false;
    boolean foundQ = false;

    public Node lowestCommonAncestor(Node root, int p, int q) {
        if (root == null)
            return root;
        if (root.val == p || root.val == q) {
            foundP = root.val == p ? true : foundP;
            foundQ = root.val == q ? true : foundQ;
        }
        Node left = lowestCommonAncestor(root.left, p, q);
        Node right = lowestCommonAncestor(root.right, p, q);
        if (root.val == p || root.val == q) {
            return root;
        } else {
            if (left != null && right != null)
                return root;
            else if (left != null)
                return left;
            else if (right != null)
                return right;
            return null;
        }
    }

    public boolean findPath(Node node, Node target, ArrayList<Integer> path) {
        if (node == null)
            return false;
        if (node.equals(target)) {
            path.add(node.val);
            return true;
        }
        path.add(node.val);
        if (findPath(node.left, target, path) || findPath(node.right, target, path))
            return true;
        path.remove(path.size() - 1);
        return false;
    }

    public void lca(Node node, int p, int q) {
        Node lca = lowestCommonAncestor(node, p, q);
        if (foundP && foundQ) {
            ArrayList<Integer> path = new ArrayList<>();
            findPath(node, lca, path);
            for (int x : path) {
                System.out.print(x + " ");
            }
        }
    }
}
```


---

# 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:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/print-common-nodes-on-path-from-root-or-common-ancestors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
