美文网首页
每日一题-leetcode 二叉树的层序遍历 II

每日一题-leetcode 二叉树的层序遍历 II

作者: 程序员小2 | 来源:发表于2022-05-18 09:23 被阅读0次

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

示例 1:

image.png

输入:root = [3,9,20,null,null,15,7]
输出:[[15,7],[9,20],[3]]
示例 2:

输入:root = [1]
输出:[[1]]
示例 3:

输入:root = []
输出:[]

提示:

树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000

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>> levelOrder = new LinkedList<List<Integer>>();
       if (root == null) {
           return levelOrder;
       }
       Queue<TreeNode> queue = new LinkedList<TreeNode>();
       queue.offer(root);
       while (!queue.isEmpty()) {
           List<Integer> level = new ArrayList<Integer>();
           int size = queue.size();
           for (int i = 0; i < size; i++) {
               TreeNode node = queue.poll();
               level.add(node.val);
               TreeNode left = node.left, right = node.right;
               if (left != null) {
                   queue.offer(left);
               }
               if (right != null) {
                   queue.offer(right);
               }
           }
           levelOrder.add(0, level);
       }
       return levelOrder;
   }
}

相关文章

网友评论

      本文标题:每日一题-leetcode 二叉树的层序遍历 II

      本文链接:https://www.haomeiwen.com/subject/zcoourtx.html