LeetCode题库解答与分析——#107. 二叉树的层次遍历 IIBinaryTreeLevelOrderTraversal

原创 2018年04月17日 09:24:15

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

例如:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其自自底向上的层次遍历为:

[
  [15,7],
  [9,20],
  [3]
]

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 traversal as:

[
  [15,7],
  [9,20],
  [3]
]

个人思路:

参考二叉树的层次遍历的思路,但总的结果用栈存储,最后再一次弹出,从而使各个分数组逆序排列

代码(JavaScript):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        Stack<List<Integer>> visited=new Stack<List<Integer>>();
        List<List<Integer>> result=new LinkedList<List<Integer>>();
        int row=0,col=0;
        if(root!=null){queue.offer(root);}
        else{return visited;}
        while(!queue.isEmpty()){
            int level=queue.size();
            List<Integer> sub_visited=new LinkedList<Integer>();
            for(int i=0;i<level;i++){
                if(queue.peek().left!=null){queue.offer(queue.peek().left);}
                if(queue.peek().right!=null){queue.offer(queue.peek().right);}
                sub_visited.add(queue.poll().val);
            }
            visited.push(sub_visited);
        }
        while(!visited.isEmpty()){
            result.add(visited.pop());
        }
        return result;
    }
}


[leetcode-二叉树层次遍历并统计每层节点数]--107. Binary Tree Level Order Traversal II

Question 107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level...
  • u010853261
  • u010853261
  • 2017-02-09 13:35:09
  • 778

107. 二叉树的层次遍历 II

107. 二叉树的层次遍历 II class Solution { public: vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; level...
  • gl486546
  • gl486546
  • 2018-04-01 15:17:09
  • 21

[leetcode-二叉树层次遍历并统计每层节点数]--102. Binary Tree Level Order Traversal

Question 102. Binary Tree Level Order Traversal Given a binary tree, return the level order traver...
  • u010853261
  • u010853261
  • 2017-02-09 13:22:07
  • 822

leetcode 二叉树层次遍历输出Binary Tree Level Order Traversal

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left...
  • yanerhao
  • yanerhao
  • 2015-08-15 10:07:51
  • 1042

LeetCode | Binary Tree Zigzag Level Order Traversal(二叉树锯齿形层序输出)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to...
  • a45872055555
  • a45872055555
  • 2014-08-14 12:12:48
  • 455

LeetCode OJ 之 Binary Tree Level Order Traversal (二叉树的层次遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...
  • u012243115
  • u012243115
  • 2014-11-13 22:54:41
  • 481

leetcode+二叉树层次遍历+偶行需要颠倒一下

点击打开链接#include &amp;lt;iostream&amp;gt; #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;stdlib.h&a...
  • u013554860
  • u013554860
  • 2018-03-18 10:28:24
  • 12

二叉树 层次遍历 (queue)遍历的变式题(leetcode)

利用队列结构和二叉树,变换求解116. Populating Next Right Pointers in Each Node题目地址 https://leetcode.com/problems/p...
  • qq_26437925
  • qq_26437925
  • 2016-10-04 12:35:41
  • 491

(二叉树z字形层次遍历)LeetCode#103. Binary Tree Zigzag Level Order Traversal

按照z字形层次遍历二叉树
  • u012559634
  • u012559634
  • 2017-06-05 15:14:48
  • 704
收藏助手
不良信息举报
您举报文章:LeetCode题库解答与分析——#107. 二叉树的层次遍历 IIBinaryTreeLevelOrderTraversal
举报原因:
原因补充:

(最多只允许输入30个字)