二叉搜索时与双向链表python_【剑指offer】26 二叉搜索树与双向链表
生活随笔
收集整理的這篇文章主要介紹了
二叉搜索时与双向链表python_【剑指offer】26 二叉搜索树与双向链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-?題目描述
輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只能調整樹中結點指針的指向。
-?解題思路
遞歸
-?Java實現
/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }*/public class Solution { private TreeNode pre = null; private TreeNode head = null; public TreeNode Convert(TreeNode root) { inOrder(root); return head; } private void inOrder(TreeNode node) { if (node == null) return; inOrder(node.left); node.left = pre; if (pre != null) pre.right = node; pre = node; if (head == null) head = node; inOrder(node.right); }}總結
以上是生活随笔為你收集整理的二叉搜索时与双向链表python_【剑指offer】26 二叉搜索树与双向链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【js】获得项目路径
- 下一篇: 2015/10/9 Python核编初级