LeetCode筆記:102. Binary Tree Level Order Traversal

問(wèn)題:

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],


image.png

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

大意:

給出一個(gè)二叉樹,返回層級(jí)順序的節(jié)點(diǎn)值(從左到右,一層層的)。

例子:
給出二叉樹 [3,9,20,null,null,15,7]。


image.png

返回他的層級(jí)順序?yàn)椋?/p>

[
  [3],
  [9,20],
  [15,7]
]

思路:

這道題和LeetCode筆記:107. Binary Tree Level Order Traversal II是姊妹題,解題思路都是一樣的,只是結(jié)果要求的順序是反的,同樣有兩種方法,也就是經(jīng)常說(shuō)到的DFS深度優(yōu)先遍歷和BFS廣度優(yōu)先遍歷。

BFS:
廣度優(yōu)先遍歷就是一層層地攻略過(guò)去,把每一層的所有節(jié)點(diǎn)都記錄下來(lái)再走向下一層。因?yàn)槊繉訒?huì)有多個(gè)節(jié)點(diǎn),不是簡(jiǎn)單的一個(gè)左節(jié)點(diǎn)一個(gè)右節(jié)點(diǎn)的,所以這里用到隊(duì)列,用隊(duì)列的先進(jìn)先出特性來(lái)記錄每一層的節(jié)點(diǎn),保證對(duì)每層的每個(gè)節(jié)點(diǎn)都處理到其子節(jié)點(diǎn),并將值記錄下來(lái)。隊(duì)列用到Queue這個(gè)類,offer方法可以添加一個(gè)元素,peek方法獲取隊(duì)首的元素,poll方法會(huì)從隊(duì)首移除一個(gè)元素并獲取它。

DFS:
深度優(yōu)先遍歷一般用遞歸來(lái)實(shí)現(xiàn),也就是對(duì)每個(gè)方向都用遞歸來(lái)往下找子節(jié)點(diǎn),先用一個(gè)空的List占個(gè)位置,這一層每找到一個(gè)都添加到這個(gè)位置的List中去,一直找到最底層為止。

這個(gè)題目里BFS會(huì)比DFS快一點(diǎn)點(diǎn)。

代碼(Java):

BFS:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        
        if (root == null) return result;
        
        queue.offer(root);
        while (!queue.isEmpty()) {
            int levelNum = queue.size();
            List<Integer> subList = new LinkedList<Integer>();
            for (int i = 0; i < levelNum; i++) {
                if (queue.peek().left != null) queue.offer(queue.peek().left);
                if (queue.peek().right != null) queue.offer(queue.peek().right);
                subList.add(queue.poll().val);
            }
            result.add(subList);
        }
        
        return result;
    }
}

DFS:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        levelMaker(result, root, 0);
        return result;
    }
    
    public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
        if (root == null) return;
        if (level >= list.size()) {
            list.add(level, new LinkedList<Integer>());
        }
        levelMaker(list, root.left, level+1);
        levelMaker(list, root.right, level+1);
        list.get(level).add(root.val);
    }
}

合集:https://github.com/Cloudox/LeetCode-Record


查看作者首頁(yè)

?著作權(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)容

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