/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    TreeNode head,pre;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return null;
        dfs(pRootOfTree);
//         head.left = pre;
//         pre.right = head;
        return head;
    }
    public void dfs(TreeNode root){
        if(root.left != null)dfs(root.left);
        if(pre != null)pre.right = root;
        else head = root;
        root.left = pre;
        pre = root;
        if(root.right != null)dfs(root.right);
    }
    
}