樹的理解(三):遍歷

層次遍歷

https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()) {
            int count = q.size();   //每層的節點數
            List<Integer> list = new ArrayList<>();
            while(count > 0) {
                TreeNode cur = q.poll();
                if(cur.left != null)
                    q.offer(cur.left);
                if(cur.right != null)
                    q.offer(cur.right);
                list.add(cur.val);
                count--;
            }
            res.add(list);
        }
        return res;
    }
}

二叉樹的層平均值

https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/

class Solution {
    private List<Double> res = new ArrayList<>();
    public List<Double> averageOfLevels(TreeNode root) {
        if(root == null) return res;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()) {
            int count = q.size();
            double sum = 0;
            int cur_count =  count;
            while(count-- > 0) {
                TreeNode cur = q.poll();
                sum += cur.val;
                if(cur.left != null) q.offer(cur.left);
                if(cur.right != null) q.offer(cur.right);
            }
            res.add(sum / cur_count);
        }
        return res;
    }
}

找樹左下角的值

https://leetcode-cn.com/problems/find-bottom-left-tree-value/

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int res = root.val;
        while(!q.isEmpty()) {
            int count = q.size();
            int flag = 0;
            while(count-- > 0) {
                TreeNode cur = q.poll();
                if(flag == 0) {
                    res = cur.val;
                    flag = 1;
                }
                if(cur.left != null) q.offer(cur.left);
                if(cur.right != null) q.offer(cur.right);
            }
        }
        return res;
    }
}

二叉樹的前序遍歷(非遞歸)

public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null){
            while(root != null) {
                stack.push(root);
                list.add(root.val);
                root = root.left;
            }
            TreeNode cur = stack.pop();
            root = cur.right;
        }
        return list;
    }

二叉樹的中序遍歷(非遞歸)

public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            TreeNode cur = stack.pop();
            list.add(cur.val);
            root = cur.right;
        }
        return list;
    }

二叉樹的後序遍歷(非遞歸)

public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> visited = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = root;
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.peek();
            if(root.right == pre || root.right == null) {
                stack.pop();
                visited.add(root.val);
                pre = root;
                root = null;
            }else 
                root = root.right;
        }
        return visited;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章