二叉樹之字形(Z字型)遍歷(LeetCode#103. Binary Tree Zigzag Level Order Traversal)

  • 題目:按照z字形層次遍歷二叉樹(以根節點所在層爲第1層,則第二層的變量從右邊節點開始直到最左邊節點,第三層遍歷則是從最左邊開始到最右邊)
  • 思路:z字形層次遍歷是對層次遍歷加上了一個限制條件(即相鄰層,從左到右的遍歷順序相反),因此還是可以採用隊列來實現,只不過節點接入隊列時需要考慮加入的順序
  • 代碼:
    對節點之間的順序進行維護
public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(root == null){
            return result;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        int depth = 0;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> tmp = new ArrayList<>();
            for(int i = 0; i < size; i++){
                TreeNode node = null;
                //因爲是走z字形,所有相鄰兩層的節點處理是相反的
                if(depth%2 == 0){
                    node = queue.pollLast();//獲取鏈表最後一個節點
                    if(node.left != null){
                        queue.offerFirst(node.left);
                    }
                    if(node.right != null){
                        queue.offerFirst(node.right);
                    }

                }else{
                    node = queue.poll();//獲取鏈表第一個節點
                    if(node.right != null){
                        queue.offer(node.right);
                    }
                    if(node.left != null){
                        queue.offer(node.left);
                    }
                }
                tmp.add(node.val);
            }
            depth++;
            result.add(tmp);
        }
        return result;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

對節點值之間進行維護,關注點在於值的順序

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(root == null){
            return result;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        int depth = 0;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            LinkedList<Integer> tmp = new LinkedList<>();//這裏不能申明爲將LinkedList泛化爲list,否則不能調用addFirst方法
            for(int i = 0; i < size; i++){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
                if(depth%2 == 0){
                    tmp.add(node.val);
                }else{
                    tmp.addFirst(node.val);
                }
            }
            depth++;
            result.add(tmp);
        }
        return result;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

如果不把重點放在節點之間的順序,可將迭代的實現改成相應的遞歸實現

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<LinkedList<Integer>> list = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        helper(list, root, 0);
        result.addAll(list);
        return result;
    }
    public void helper(List<LinkedList<Integer>> result, TreeNode node, int depth){
        if(node == null){
            return;
        }
        if(depth == result.size()){
            result.add(new LinkedList());
        }
        if(depth%2 == 0){
            result.get(depth).add(node.val);
        }else{
            result.get(depth).addFirst(node.val);
        }
        helper(result, node.left, depth+1);
        helper(result, node.right, depth+1);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
發佈了72 篇原創文章 · 獲贊 45 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章