|
|
|
@ -14,6 +14,9 @@ |
|
|
|
|
8. [32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) |
|
|
|
|
9. [32 - III. 从上到下打印二叉树 III](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/) |
|
|
|
|
10. [37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) |
|
|
|
|
11. [28. 对称的二叉树](https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/) |
|
|
|
|
12. [33. 二叉搜索树的后序遍历序列](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/) |
|
|
|
|
13. [Offer 36. 二叉搜索树与双向链表](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/) |
|
|
|
|
|
|
|
|
|
#### [07. 重建二叉树](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/) |
|
|
|
|
|
|
|
|
@ -510,3 +513,42 @@ class Solution { |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
#### [Offer 36. 二叉搜索树与双向链表](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/) |
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
class Solution { |
|
|
|
|
|
|
|
|
|
public Node treeToDoublyList(Node root) { |
|
|
|
|
if (root == null) { |
|
|
|
|
return root; |
|
|
|
|
} |
|
|
|
|
List<Node> list = new ArrayList<>(); |
|
|
|
|
helper(list, root); |
|
|
|
|
Node head = list.get(0); |
|
|
|
|
Node pre = head; |
|
|
|
|
for (int i = 1; i < list.size(); i++) { |
|
|
|
|
Node node = list.get(i); |
|
|
|
|
pre.right = node; |
|
|
|
|
node.left = pre; |
|
|
|
|
pre = pre.right; |
|
|
|
|
} |
|
|
|
|
pre.right = head; |
|
|
|
|
head.left = pre; |
|
|
|
|
return head; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void helper(List<Node> list, Node root) { |
|
|
|
|
if (root == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (root.left != null) { |
|
|
|
|
helper(list, root.left); |
|
|
|
|
} |
|
|
|
|
list.add(root); |
|
|
|
|
if (root.right != null) { |
|
|
|
|
helper(list, root.right); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|