Ez understanding JAVA solution using post order. No public variable


  • 0
    A

    public class Solution {

    public void flatten(TreeNode root) {//preorder
        if(root == null || (root.left == null && root.right == null)){
            return;
        }
        flatten(root.left);
        flatten(root.right);
        if(root.left != null){//if root.left == null, just do nothing
            TreeNode temp = root.right;
            root.right = root.left;
            root.left = null;
            
            TreeNode i = root;
            while(i.right!=null){
                i = i.right;
            }
            i.right = temp;
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.