关闭

【Leetcode】Binary Tree Level Order Traversal II

276人阅读 评论(0) 收藏 举报
分类:

题目链接: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 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 traversal as:

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

思路:

定义一个方法 根据传入的层数返回该层所有结点。自下而上的查找各层结点。

算法:

	public List<List<Integer>> levelOrderBottom(TreeNode root) {
		int height = heightTree(root);
		List<List<Integer>> lists = new ArrayList<List<Integer>>();
		for (int i = height; i > 0; i--) { //自下而上
			List<Integer> list = new ArrayList<Integer>();
			list = kLevelNumber(root, 1, list, i);
			lists.add(list);
		}
		return lists;
	}

	public int heightTree(TreeNode p) {
		if (p == null)
			return 0;
		int h1 = heightTree(p.left);
		int h2 = heightTree(p.right);
		return h1 > h2 ? h1 + 1 : h2 + 1;
	}

	/**
	 * 返回kk层所有结点 
	 */
	public List<Integer> kLevelNumber(TreeNode p, int height, List<Integer> list, int kk) {
		if (p != null) {
			if (height == kk) {
				list.add(p.val);
			}
			list = kLevelNumber(p.left, height + 1, list, kk);
			list = kLevelNumber(p.right, height + 1, list, kk);
		}
		return list;
	}


0
0
查看评论

LeetCode --- 107. Binary Tree Level Order Traversal II

题目链接:Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
  • makuiyu
  • makuiyu
  • 2015-04-17 16:46
  • 1852

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 ...
  • sunao2002002
  • sunao2002002
  • 2015-05-31 00:11
  • 3208

[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...
  • yangjin95
  • yangjin95
  • 2016-07-24 17:39
  • 250

leetCode解题报告之Binary Tree Level Order Traversal II,I(二叉树层次遍历)

题目: Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fr...
  • u011133213
  • u011133213
  • 2014-03-28 20:28
  • 14696

leetcode 107 Binary Tree Level Order Traversal II C++

层次遍历,本来一个循环就可以,但是要输出一个二位数组,就变成嵌套循环了。 然后把结果逆序一下就可以了。 class Solution { public: vector> levelOrderBottom(TreeNode* root) { vector> re...
  • a2331046
  • a2331046
  • 2016-05-28 20:57
  • 396

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

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given ...
  • crazy1235
  • crazy1235
  • 2016-05-26 16:18
  • 6271

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

题目:Binary Tree Level Order Traversal - 二叉树层次遍历 (DFS | BFS) Given a binary tree, return the level order traversal of its nodes' values. (ie, from l...
  • Eastmount
  • Eastmount
  • 2015-09-11 02:20
  • 2762

102. Binary Tree Level Order Traversal [easy] (Python)

题目链接https://leetcode.com/problems/binary-tree-level-order-traversal/题目原文 Given a binary tree, return the level order traversal of its nodes’ values....
  • coder_orz
  • coder_orz
  • 2016-05-10 14:40
  • 1290

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

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a b...
  • crazy1235
  • crazy1235
  • 2016-05-26 15:06
  • 2784

【LeetCode with Python】 Binary Tree Level Order Traversal

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 t...
  • nerv3x3
  • nerv3x3
  • 2014-07-06 14:12
  • 1736
    个人资料
    • 访问:567711次
    • 积分:8659
    • 等级:
    • 排名:第2678名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论