• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java flatten binary tree to linked list

hj1

New Coder

what does flatten recursion statements specifically do here​



flatten{
if (root==null)
return;



TreeNode right = root.right;
flatten(root.left);



root.right = root.left;
root.left = null;



while(root.right!=null){ root = root.right;
flatten(right);
root.right = right;}
 

Malcolm

Administrator
Administrator
Verified
Hello, Welcome to Code Forum!

Please add ALL CODE within our BBCode Feature (</>). It is required when posting any code within a thread. You can view more here.

Not familiar with Java, but that if statement doesn't look correct.. However I may be wrong. An if statement should look similar to:

Java:
if (condition) {
   statement
}

if (root==null)
return;
This may mean that if root equals to null then return.

TreeNode right = root.right;
flatten(root.left);
This may mean that TreeNode right = root.right; which means that root.right will have whatever value root.right has.

root.right = root.left;
root.left = null;
This looks that root.right is being assigned the root.left value then root.left is being set to null.

while(root.right!=null){ root = root.right;
flatten(right);
root.right = right;}
This appears to be while root.right is NOT null root equals to root.right value, and flatten is right and root.right is also right.

Please keep in mind this is just my two cents I have no idea if that's correct.

But what it does I couldn't tell you. @Ghost @Krusty the Senile @simong1993 @ivan.moony @Tealk You guys know what this means?
 
Last edited:
Hi hj1,

I formatted the code a bit and got this

Code:
flatten {
    if (root==null)
        return;

    TreeNode right = root.right;
    flatten(root.left);

    root.right = root.left;
    root.left = null;

    while(root.right!=null) {
        root = root.right;
        flatten(right);
        root.right = right;
    }

Assuming this is a method of existing class, there are errors here:
  • line 1: missing method type and method parameters
  • lines 2, 5, 6, 8, 9, 11, 12, 14: undefined variable `root` (maybe this is a method parameter?)
  • line 16: expected `}`, or the rest of the code, possibly including `return` statement (does the code really end here?)
Where did you get this code from?

Anyway, I found this nice article about flattening a tree to an array (that's analogous to linked list). Click on "Java" tab to see the java code instead of C++ code. The article provides a graphical explanation of flattening algorithm. Does that article answer your question? Is there anything you don't understand, maybe we can help explaining it?
 
Last edited:
Top Bottom