/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { TreeNode head; TreeNode pre = null; public TreeNode Convert(TreeNode pRootOfTree) { if(null == pRootOfTree){ return null; } transport(pRootOfTree); return head; } public void transport(TreeNode cur){ //左节点不为空,则一直往下找 if(cur.left != null){ transport(cur.left); } //更新当前节点的左边位置 cur.left = pre; //更新上一个节点的右边位置。只有第一个节点没有左节点 if(pre != null){ pre.right = cur; }else{ //只有第一个节点没有左节点。 存一下链表头 head = cur; } //将当前节点更新为前一个节点 pre = cur; //更新右子树 if(cur.right != null){ transport(cur.right); } } }
原文:https://www.cnblogs.com/MoonBeautiful/p/13057620.html