能想到是中序遍历,但是链表的构造没有想到
题解是类似指针标记
class Solution { Node pre,head; public Node treeToDoublyList(Node root) { if(root == null) return null; dfs(root); head.left = pre; pre.right = head; return head; } void dfs(Node root){ if(root == null) return ; dfs(root.left); if(pre == null) head = root; else pre.right = root; root.left = pre; pre = root; dfs(root.right); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)