给你二叉树的根结点 root ,请你将它展开为一个单链表:
展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
展开后的单链表应该与二叉树 先序遍历 顺序相同。
1 public void flatten(TreeNode root) { 2 if(root == null){ 3 return; 4 } 5 6 TreeNode cur = root; 7 while(cur != null){ 8 if(cur.left != null){ 9 TreeNode next = cur.left; 10 TreeNode temp = next; 11 while(temp.right != null){ 12 temp = temp.right; 13 } 14 temp.right = cur.right; 15 cur.left = null; 16 cur.right = next; 17 } 18 cur = cur.right; 19 } 20 } 21 22}
由 好买网提供
更多建站及源码交易信息请见 GoodMai
