Not optimal but easy understanding


  • 0
    N
    public class Solution {
        public void flatten(TreeNode root) {
     		TreeNode runner=root;
     		while(runner!=null){
     			TreeNode temp=runner.right;
     			runner.right=runner.left;
     			runner.left=null;
     			TreeNode next=RightMost(runner);
     			next.right=temp;
     			runner=runner.right;
     		}       
        }
    
        public TreeNode RightMost(TreeNode root){
        	if(root.right==null) return root;
        	else 
        	    return RightMost(root.right);
        }
    }

Log in to reply
 

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