Leet Code OJ 107. Binary Tree Level Order Traversal II [Difficulty: Easy]

题目:

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:

Given binary tree {3,9,20,#,#,15,7},

return its bottom-up level order traversal as:

[

[15,7],

[9,20],

[3]

]

翻译:

给定一个二叉树,返回它的节点的从底部到头部的层序遍历结果。

代码:

/**
 * 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>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result=levelOrder(root);
        Collections.reverse(result);
        return result;
    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        List<Integer> one=new ArrayList<>();
        one.add(root.val);
        result.add(one);
        List<List<Integer>> left=levelOrder(root.left);
        List<List<Integer>> right=levelOrder(root.right);
        for(int i=0;i<left.size()||i<right.size();i++){
            List<Integer> item=new ArrayList<>();
            if(i<left.size()){
                for(Integer k:left.get(i)){
                    item.add(k);
                }
            }
            if(i<right.size()){
                for(Integer k:right.get(i)){
                    item.add(k);
                }
            }
            result.add(item);
        }
        return result;
    }
}
时间: 2024-12-09 00:28:38

Leet Code OJ 107. Binary Tree Level Order Traversal II [Difficulty: Easy]的相关文章

Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]

题目: 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,#,#,15,7}, return its level order traversal as: [ [3], [9,20], [15,7] ] 翻译: 给定一个二叉树,返回它的节

LeetCode OJ 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7],     3  

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【LeetCode】107. Binary Tree Level Order Traversal II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51508308 Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to rig

(二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

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

107. Binary Tree Level Order Traversal II

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

107. Binary Tree Level Order Traversal II Java Solutions

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave