先序中序建樹(shù)锈死、后序中序建樹(shù)

一.先序中序建樹(shù)

image.png

思路:根據(jù)先序遍歷數(shù)組的元素從左到右確定根結(jié)點(diǎn),先構(gòu)建左子樹(shù)后構(gòu)建右子樹(shù)缕溉。
中序遍歷數(shù)組確定左右子樹(shù)以及左右子樹(shù)的節(jié)點(diǎn)個(gè)數(shù)。

class TreeNode{
    public int val;
    public TreeNode left = null;
    public TreeNode right = null;

    TreeNode(int val){
        this.val = val;
    }
}

public class Solution {
    public TreeNode recreateTree(int [] pre,int [] in, int preBegin, int preEnd,
                                 int inBegin, int inEnd){
        if (preBegin > preEnd){return null;}

        TreeNode root = new TreeNode(pre[preBegin]);

        int k = inBegin;

        while (in[k] != pre[preBegin]){
            k++;
        }

        root.left = recreateTree(pre, in, preBegin + 1, preBegin + k - inBegin, inBegin, k - 1);
        root.right = recreateTree(pre, in, preEnd - inEnd + k + 1, preEnd, k + 1, inEnd);
        return root;
    }

    public void printIn(TreeNode root){
        if (root == null){return;}
        printIn(root.left);
        System.out.print(root.val + " ");
        printIn(root.right);
    }

    public void printPost(TreeNode root){
        if (root == null){return;}
        printPost(root.left);
        printPost(root.right);
        System.out.print(root.val + " ");
    }

    public void printPre(TreeNode root){
        if (root == null){return;}
        System.out.print(root.val + " ");
        printPre(root.left);
        printPre(root.right);
    }

    public static void main(String[] args){
       int[] pre = {1, 3, 6, 7, 5, 8, 4};
       int[] in = {6, 3, 7, 1, 8, 4, 5};

       TreeNode root = new Solution().recreateTree(pre, in, 0, 6, 0, 6);
       System.out.print("先序遍歷:");
       new Solution().printPre(root);
       System.out.println();
       System.out.print("中序遍歷:");
       new Solution().printIn(root);
        System.out.println();

        System.out.print("后序遍歷:");
       new Solution().printPost(root);
        System.out.println();
    }
}
image.png

二.后序中序建樹(shù)

image.png

思路:根據(jù)后序遍歷數(shù)組的元素從右到左確定根結(jié)點(diǎn)吃型,并且先構(gòu)建右子樹(shù)证鸥,后構(gòu)建左子樹(shù)。
中序遍歷數(shù)組確定左右子樹(shù)以及左右子樹(shù)的節(jié)點(diǎn)個(gè)數(shù)勤晚。

class TreeNode{
    int val;
    TreeNode left = null;
    TreeNode right = null;

    TreeNode(int val){
        this.val = val;
    }
}

public class Solution {
    public TreeNode createTree(int[] in, int[] post, int inBegin, int inEnd, int postBegin, int postEnd){
        if (postEnd < postBegin){return null;}

        int k = inBegin;

        while (in[k] != post[postEnd]){
            k++;
        }

        TreeNode root = new TreeNode(post[postEnd]);        //k - inBegin

        root.right = createTree(in, post, k + 1, inEnd, postEnd + k - inEnd, postEnd-1);
        root.left = createTree(in, post, inBegin, k - 1, postBegin, postBegin + k - inBegin - 1);
        return root;
    }

    public void printIn(TreeNode root){
        if (root == null){return;}
        printIn(root.left);
        System.out.print(root.val + " ");
        printIn(root.right);
    }

    public void printPost(TreeNode root){
        if (root == null){return;}
        printPost(root.left);
        printPost(root.right);
        System.out.print(root.val + " ");
    }

    public void printPre(TreeNode root){
        if (root == null){return;}
        System.out.print(root.val + " ");
        printPre(root.left);
        printPre(root.right);
    }

    public static void main(String[] args){
       int[] in = {6, 3, 7, 1, 8, 4, 5};
       int[] post = {6, 7, 3, 4, 8, 5, 1};

       TreeNode root = new Solution().createTree(in, post, 0, 6, 0, 6);
       System.out.print("先序遍歷:");
       new Solution().printPre(root);
       System.out.println();
       System.out.print("中序遍歷:");
       new Solution().printIn(root);
        System.out.println();

        System.out.print("后序遍歷:");
       new Solution().printPost(root);
        System.out.println();
    }
}
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末枉层,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子赐写,更是在濱河造成了極大的恐慌鸟蜡,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挺邀,死亡現(xiàn)場(chǎng)離奇詭異揉忘,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)端铛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門泣矛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人沦补,你說(shuō)我怎么就攤上這事乳蓄。” “怎么了夕膀?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵虚倒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我产舞,道長(zhǎng)魂奥,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任易猫,我火速辦了婚禮耻煤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己哈蝇,他們只是感情好棺妓,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著炮赦,像睡著了一般怜跑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吠勘,一...
    開(kāi)封第一講書(shū)人閱讀 48,970評(píng)論 1 284
  • 那天性芬,我揣著相機(jī)與錄音,去河邊找鬼剧防。 笑死植锉,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的峭拘。 我是一名探鬼主播俊庇,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼棚唆!你這毒婦竟也來(lái)了暇赤?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤宵凌,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后止后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體瞎惫,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年译株,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓜喇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡歉糜,死狀恐怖乘寒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情匪补,我是刑警寧澤伞辛,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站夯缺,受9級(jí)特大地震影響蚤氏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜踊兜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一竿滨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦于游、人聲如沸毁葱。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)倾剿。三九已至,卻和暖如春鸠澈,著一層夾襖步出監(jiān)牢的瞬間柱告,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工笑陈, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留际度,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓涵妥,卻偏偏與公主長(zhǎng)得像乖菱,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蓬网,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容