so i wrote this function to convert from generic to binary tree by taking the root and…
- User
so i wrote this function to convert from generic to binary tree by taking the root and change it, now i want to write an exact opposite to that function that will take the root of the binary tree and convert it to generic and then return the root of the generic tree: public static TreeNode genericToBinary(TreeNode root) { if (root == null || root.children.size() == 0) { return root; }
coderoot.right = genericToBinary(root.children.get(root.children.size() - 1)); TreeNode curr = root.right; for (int i = root.children.size() - 2; i > -1; i--) { curr.left = genericToBinary(root.children.get(i)); curr = curr.left; } return root; } - Assistant
- User
you should know that the right child of a binary tree is the rightmost child of the generic tree and the left child of a binary tree is the left sibling of that node in the generic tree, change the code accordingly
- Assistant