欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java開(kāi)發(fā)深入分析講解二叉樹(shù)的遞歸和非遞歸遍歷方法

 更新時(shí)間:2022年05月16日 10:17:50   作者:有什么奇怪!  
樹(shù)是一種重要的非線(xiàn)性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹(shù)中稱(chēng)為結(jié)點(diǎn))按分支關(guān)系組織起來(lái)的結(jié)構(gòu),很象自然界中的樹(shù)那樣。樹(shù)結(jié)構(gòu)在客觀世界中廣泛存在,如人類(lèi)社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹(shù)形象表示,本篇介紹二叉樹(shù)的遞歸與非遞歸遍歷的方法

前言

二叉樹(shù)的遍歷方法分為前序遍歷,中序遍歷,后續(xù)遍歷,層序遍歷。

1.遞歸遍歷

對(duì)于遞歸,就不得不說(shuō)遞歸三要素:以前序遍歷為例

遞歸入?yún)?shù)和返回值

因?yàn)橐蛴〕銮靶虮闅v節(jié)點(diǎn)的數(shù)值,所以參數(shù)里需要傳入List在放節(jié)點(diǎn)的數(shù)值,除了這一點(diǎn)就不需要在處理什么數(shù)據(jù)了也不需要有返回值,所以遞歸函數(shù)返回類(lèi)型就是void,代碼如下:

public void preorder(TreeNode root, List<Integer> result)

確定終止條件

在遞歸的過(guò)程中,如何算是遞歸結(jié)束了呢,當(dāng)然是當(dāng)前遍歷的節(jié)點(diǎn)是空了,那么本層遞歸就要要結(jié)束了,所以如果當(dāng)前遍歷的這個(gè)節(jié)點(diǎn)是空,就直接return

if (root == null) return;

單層循環(huán)邏輯

前序遍歷是中左右的循序,所以在單層遞歸的邏輯,是要先取中節(jié)點(diǎn)的數(shù)值,代碼如下:

result.add(root.val);
preorder(root.left, result);
preorder(root.right, result);
// 前序遍歷·遞歸·LC144_二叉樹(shù)的前序遍歷
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        preorder(root, result);
        return result;
    }
    public void preorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add(root.val);//先保存中間節(jié)點(diǎn)
        preorder(root.left, result); //處理左邊節(jié)點(diǎn)
        preorder(root.right, result); //處理右邊節(jié)點(diǎn)
    }
}
// 中序遍歷·遞歸·LC94_二叉樹(shù)的中序遍歷
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inorder(root, res);
        return res;
    }
    void inorder(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        inorder(root.left, list); //先處理左邊節(jié)點(diǎn)
        list.add(root.val);       //保存中間當(dāng)前的節(jié)點(diǎn)
        inorder(root.right, list);//先處理右邊節(jié)點(diǎn)
    }
}
// 后序遍歷·遞歸·LC145_二叉樹(shù)的后序遍歷
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        postorder(root, res);
        return res;
    }
    void postorder(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        postorder(root.left, list);  //先處理左邊節(jié)點(diǎn)
        postorder(root.right, list); //再處理右邊節(jié)點(diǎn)
        list.add(root.val);          //保存最后  
    }
}

2.非迭代遍歷

//前序遍歷
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack();
        if (root == null) return res;
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.right != null) { //先將右孩子入棧,因?yàn)樗谧詈?
                stack.push(node.right);
            }
            if (node.left != null) { //左孩子入棧再出棧
                stack.push(node.left);
            }
        }
        return res;
    }
}
//中序遍歷
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            //如果可以,一直往左下探
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop(); //彈出來(lái)的肯定是葉子節(jié)點(diǎn)或中間節(jié)點(diǎn)
                res.add(cur.val); //將這個(gè)節(jié)點(diǎn)加入list
                cur = cur.right; //查看當(dāng)前節(jié)點(diǎn)是否有右節(jié)點(diǎn),如果右,肯定是中間節(jié)點(diǎn),如果沒(méi)有,就是葉子節(jié)點(diǎn),繼續(xù)彈出就可以
            }
        }
        return res;
    }
}
//后序遍歷
//再來(lái)看后序遍歷,先序遍歷是中左右,后續(xù)遍歷是左右中,那么我們只需要調(diào)整一下先序遍歷的代碼順序,就變成中右左的遍歷順序,然后在反轉(zhuǎn)result數(shù)組,輸出的結(jié)果順序就是左右中
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.left != null) stack.push(node.left); // 相對(duì)于前序遍歷,這更改一下入棧順序 (空節(jié)點(diǎn)不入棧)
            if (node.right != null) stack.push(node.right);// 空節(jié)點(diǎn)不入棧 
        }
        Collections.reverse(res); // 將結(jié)果反轉(zhuǎn)之后就是左右中的順序了
        return res;
    }
}

3.二叉樹(shù)的統(tǒng)一迭代法

//前序遍歷
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        Stack<TreeNode> st = new Stack<>();
        if (root != null) st.push(root);
        while (!st.empty()) {
            TreeNode node = st.peek();
            if (node != null) {
                st.pop(); // 將該節(jié)點(diǎn)彈出,避免重復(fù)操作,下面再將右中左節(jié)點(diǎn)添加到棧中
                if (node.right!=null) st.push(node.right);  // 添加右節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)
                if (node.left!=null) st.push(node.left);    // 添加左節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)
                st.push(node);                          // 添加中節(jié)點(diǎn)
                st.push(null); // 中節(jié)點(diǎn)訪問(wèn)過(guò),但是還沒(méi)有處理,加入空節(jié)點(diǎn)做為標(biāo)記。
            } else { // 只有遇到空節(jié)點(diǎn)的時(shí)候,才將下一個(gè)節(jié)點(diǎn)放進(jìn)結(jié)果集
                st.pop();           // 將空節(jié)點(diǎn)彈出
                node = st.peek();    // 重新取出棧中元素
                st.pop();
                result.add(node.val); // 加入到結(jié)果集
            }
        }
        return result;
    }
}
//中序遍歷
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        Stack<TreeNode> st = new Stack<>();
        if (root != null) st.push(root);
        while (!st.empty()) {
            TreeNode node = st.peek();
            if (node != null) {
                st.pop(); // 將該節(jié)點(diǎn)彈出,避免重復(fù)操作,下面再將右中左節(jié)點(diǎn)添加到棧中
                if (node.right!=null) st.push(node.right);  // 添加右節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)
                st.push(node);                          // 添加中節(jié)點(diǎn)
                st.push(null); // 中節(jié)點(diǎn)訪問(wèn)過(guò),但是還沒(méi)有處理,加入空節(jié)點(diǎn)做為標(biāo)記。
                if (node.left!=null) st.push(node.left);    // 添加左節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)
            } else { // 只有遇到空節(jié)點(diǎn)的時(shí)候,才將下一個(gè)節(jié)點(diǎn)放進(jìn)結(jié)果集
                st.pop();           // 將空節(jié)點(diǎn)彈出
                node = st.peek();    // 重新取出棧中元素
                st.pop();
                result.add(node.val); // 加入到結(jié)果集
            }
        }
        return result;
    }
}
//后序遍歷
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        Stack<TreeNode> st = new Stack<>();
        if (root != null) st.push(root);
        while (!st.empty()) {
            TreeNode node = st.peek();
            if (node != null) {
                st.pop(); // 將該節(jié)點(diǎn)彈出,避免重復(fù)操作,下面再將右中左節(jié)點(diǎn)添加到棧中
                st.push(node);                          // 添加中節(jié)點(diǎn)
                st.push(null); // 中節(jié)點(diǎn)訪問(wèn)過(guò),但是還沒(méi)有處理,加入空節(jié)點(diǎn)做為標(biāo)記。
                if (node.right!=null) st.push(node.right);  // 添加右節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)
                if (node.left!=null) st.push(node.left);    // 添加左節(jié)點(diǎn)(空節(jié)點(diǎn)不入棧)         
            } else { // 只有遇到空節(jié)點(diǎn)的時(shí)候,才將下一個(gè)節(jié)點(diǎn)放進(jìn)結(jié)果集
                st.pop();           // 將空節(jié)點(diǎn)彈出
                node = st.peek();    // 重新取出棧中元素
                st.pop();
                result.add(node.val); // 加入到結(jié)果集
            }
        }
        return result;
    }
}

到此這篇關(guān)于Java開(kāi)發(fā)深入分析講解二叉樹(shù)的遞歸和非遞歸遍歷方法的文章就介紹到這了,更多相關(guān)Java二叉樹(shù)的遞歸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談JackSon的幾種用法

    淺談JackSon的幾種用法

    這篇文章主要介紹了淺談JackSon的幾種用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java多線(xiàn)程實(shí)現(xiàn)之Callable詳解

    Java多線(xiàn)程實(shí)現(xiàn)之Callable詳解

    這篇文章主要介紹了Java多線(xiàn)程實(shí)現(xiàn)之Callable詳解,Callable是一個(gè)接口,用于實(shí)現(xiàn)多線(xiàn)程,與實(shí)現(xiàn)Runnable類(lèi)似,但是功能更強(qiáng)大,通過(guò)實(shí)現(xiàn)Callable接口,我們需要重寫(xiě)call()方法,該方法可以在任務(wù)結(jié)束后提供一個(gè)返回值,需要的朋友可以參考下
    2023-08-08
  • SpringBoot接口如何對(duì)參數(shù)進(jìn)行校驗(yàn)

    SpringBoot接口如何對(duì)參數(shù)進(jìn)行校驗(yàn)

    這篇文章主要介紹了SpringBoot接口如何對(duì)參數(shù)進(jìn)行校驗(yàn),在以SpringBoot開(kāi)發(fā)Restful接口時(shí),?對(duì)于接口的查詢(xún)參數(shù)后臺(tái)也是要進(jìn)行校驗(yàn)的,同時(shí)還需要給出校驗(yàn)的返回信息放到上文我們統(tǒng)一封裝的結(jié)構(gòu)中
    2022-07-07
  • apollo更改配置刷新@ConfigurationProperties配置類(lèi)

    apollo更改配置刷新@ConfigurationProperties配置類(lèi)

    這篇文章主要為大家介紹了apollo更改配置刷新@ConfigurationProperties配置類(lèi)示例解析,apollo更改配置刷新@ConfigurationProperties配置類(lèi)
    2023-04-04
  • springboot?vue接口測(cè)試前后端樹(shù)節(jié)點(diǎn)編輯刪除功能

    springboot?vue接口測(cè)試前后端樹(shù)節(jié)點(diǎn)編輯刪除功能

    這篇文章主要為大家介紹了springboot?vue接口測(cè)試前后端樹(shù)節(jié)點(diǎn)編輯刪除功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java并發(fā)編程之線(xiàn)程中斷

    Java并發(fā)編程之線(xiàn)程中斷

    這篇文章主要介紹了Java并發(fā)編程線(xiàn)程中斷,java線(xiàn)程中斷是一種線(xiàn)程間的協(xié)作模式,通過(guò)設(shè)置線(xiàn)程的中斷標(biāo)志并不能直接終止該線(xiàn)程的運(yùn)行,而是被中斷的線(xiàn)程根據(jù)中斷狀態(tài)自行處理,需要的朋友可以參考一下
    2021-09-09
  • springboot控制層圖片驗(yàn)證碼生成

    springboot控制層圖片驗(yàn)證碼生成

    這篇文章主要為大家詳細(xì)介紹了springboot控制層圖片驗(yàn)證碼生成,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Mybatis應(yīng)用mysql存儲(chǔ)過(guò)程查詢(xún)數(shù)據(jù)實(shí)例

    Mybatis應(yīng)用mysql存儲(chǔ)過(guò)程查詢(xún)數(shù)據(jù)實(shí)例

    下面小編就為大家分享一篇Mybatis應(yīng)用mysql存儲(chǔ)過(guò)程查詢(xún)數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • JdbcTemplate方法介紹與增刪改查操作實(shí)現(xiàn)

    JdbcTemplate方法介紹與增刪改查操作實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于JdbcTemplate方法與增刪改查操作實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用JdbcTemplate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能

    Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能

    這篇文章主要介紹了Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論