TreeNode node = new TreeNode(5); TreeNode parent = node.getParent();
// Method to find parent of a particular node in a tree public static TreeNode getParentNode(TreeNode root, TreeNode node) { if (root == null || node == null) { return null; } if (root.left == node || root.right == node) { return root; } TreeNode left = getParentNode(root.left, node); if (left != null) { return left; } TreeNode right = getParentNode(root.right, node); if (right != null) { return right; } return null; }In this example, a method called getParentNode() is defined that takes in two parameters - the root of a tree and a node whose parent is to be found. The method recursively traverses the tree looking for the specific node and returns its parent. Package library: The implementation of the TreeNode class is not specified in this example, so it could be from any package.