Leetcode - Convert a given Binary Tree to Doubly Linked List

這不是一個(gè)Leetcode 題目.我自己寫了下。
題意如下:
Convert a binary search tree to a sorted, circular, doubly-linked list, in place (using the tree nodes as the new list nodes).

use leftChild as "prev"

use rightChild as "next

My code:

import java.util.Comparator;
import java.util.PriorityQueue;

public class Solution {
    public TreeNode traverse(TreeNode root) {
        if (root == null)
            return null;
        return dfs(root)[0];
    }
    
    private TreeNode[] dfs(TreeNode root) {
        if (root == null)
            return null;
        TreeNode[] left = dfs(root.left);
        TreeNode head = null;
        TreeNode tail = null;
        if (left != null) {
            head = left[0];
            left[1].right = root;
            root.left = left[1];
        }
        TreeNode[] right = dfs(root.right);
        if (right != null) {
            root.right = right[0];
            right[0].left = root;
            tail = right[1];
        }
        TreeNode[] ret = new TreeNode[2];
        ret[0] = (head == null ? root : head);
        ret[1] = (tail == null ? root : tail);
        return ret;
    }
    
    
    
    public static void main(String[] args) {
        Solution test = new Solution();
        TreeNode a1 = new TreeNode(1);
        TreeNode a2 = new TreeNode(2);
        TreeNode a3 = new TreeNode(3);
        TreeNode a4 = new TreeNode(4);
        TreeNode a5 = new TreeNode(5);
        TreeNode a6 = new TreeNode(6);
        TreeNode a7 = new TreeNode(7);
        a1.left = a2;
        a1.right = a3;
        a2.left = a4;
        a2.right = a5;
        a3.left = a6;
        a3.right = a7;
        TreeNode head = test.traverse(a1);
        TreeNode tail = null;
        while (head.right != null) {
            System.out.println(head.val);
            head = head.right;
        }
        System.out.println(head.val);
        tail = head;    
        System.out.println("**************");
        while (tail != null) {
            System.out.println(tail.val);
            tail = tail.left;
        }
    }
}

自己測(cè)試了下。感覺差不多了。其實(shí)就是一個(gè)簡(jiǎn)單的dfs,in order traverse this tree.
然后每次返回一個(gè)數(shù)組,第一位放head of sub tree, 第二位放tail of sub tree
然后注意雙向鏈表的連接。

Anyway, Good luck, Richardo!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,911評(píng)論 0 33
  • 因?yàn)槭裁?,你如此平凡?一個(gè)人的時(shí)候我總是這樣問自己。 燈影搖曳,燭光映在漆黑的夜里。有溫暖的光溫暖這樣的夜。夜風(fēng)...
    xinmenglinlin閱讀 436評(píng)論 0 0
  • 1. 生活中你是否有例過下列計(jì)劃:我從現(xiàn)在開始要每天寫日記,每天跑步3-5公里,每天朗讀英語10分鐘……諸如此類計(jì)...
    賦能姐在行動(dòng)閱讀 360評(píng)論 0 1
  • 定義格式int[] a;定義一個(gè)int類型的數(shù)組a變量int a[];定義一個(gè)int類型的a數(shù)組變量推薦第一種。 ...
    Yix1a閱讀 270評(píng)論 0 0
  • 關(guān)于295社作業(yè)要求及解讀 一.題材:立冬 “立冬”是二十四節(jié)氣之一,作為題材,不要求單純地去描寫...
    詩詞海棠閱讀 499評(píng)論 0 4

友情鏈接更多精彩內(nèi)容