From 513ad76040783acc1c6a612ea2e659f6f17dd1a9 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Tue, 21 Jul 2020 08:43:43 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9B=B8?= =?UTF-8?q?=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Algorithm/剑指 Offer/二叉树相关.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/二叉树相关.md b/blogs/Algorithm/剑指 Offer/二叉树相关.md index a46d742..4172fdb 100644 --- a/blogs/Algorithm/剑指 Offer/二叉树相关.md +++ b/blogs/Algorithm/剑指 Offer/二叉树相关.md @@ -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 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 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); + } + } +} +``` +