【LeetCode刷题-树】-- 107.二叉树的层序遍历II

107.二叉树的层序遍历II

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<List<Integer>>();
        if(root==null){
            return ans;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> levelList = new ArrayList<>();
            int size = queue.size();
            for(int i = 0;i<size;i++){
                TreeNode now = queue.poll();
                levelList.add(now.val);
                if(now.left!=null){
                queue.offer(now.left);
                }
                if(now.right!=null){
                    queue.offer(now.right);
                }
            }
            //遍历完一层节点后,将存储该层节点值得列表添加到结果列表头部
            ans.add(0,levelList);
        }
        return ans;
    }
}
java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<List<Integer>>();
        if(root==null){
            return ans;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        Stack<List<Integer>> stack = new Stack<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> levelList = new ArrayList<>();
            int size = queue.size();
            for(int i = 0;i<size;i++){
                TreeNode now = queue.poll();
                levelList.add(now.val);
                if(now.left!=null){
                queue.offer(now.left);
                }
                if(now.right!=null){
                    queue.offer(now.right);
                }
            }
            //定义一个栈,每次添加时入栈
            stack.push(levelList);
        }
        //弹出元素
        while(!stack.isEmpty()){
            ans.add(stack.pop());
        }
        return ans;
    }
}
相关推荐
丶小鱼丶几秒前
二叉树算法之【Z字型层序遍历】
java·算法
岁忧2 小时前
(nice!!!)(LeetCode 每日一题) 2561. 重排水果 (哈希表 + 贪心)
java·c++·算法·leetcode·go·散列表
先做个垃圾出来………2 小时前
1. 两数之和
算法·leetcode·职场和发展
神器阿龙2 小时前
排序算法-冒泡排序
数据结构·算法·排序算法
C++ 老炮儿的技术栈2 小时前
在vscode 如何运行a.nut 程序(Squirrel语言)
c语言·开发语言·c++·ide·vscode·算法·编辑器
HKUST_ZJH3 小时前
交互 Codeforces Round 1040 Interactive RBS
c++·算法·交互
九章数学体系3 小时前
九章数学体系:打破“吃苦悖论”,重构学习真谛
数据结构·学习·算法·数学建模·拓扑学
一川月白7093 小时前
数据结构---概念、数据与数据之间的关系(逻辑结构、物理结构)、基本功能、数据结构内容、单向链表(该奶奶、对象、应用)
c语言·数据结构·算法·哈希算法·单向链表·数据关系
展信佳_daydayup3 小时前
8-1 图像增广
算法
zl_vslam4 小时前
SLAM中的非线性优化-2D图优化之零空间实战(十六)
人工智能·算法·机器学习·计算机视觉·slam se2 非线性优化