LeetCode107 Binary Tree Level Order Traversal II

原创 2015年07月06日 21:29:24

题目:

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]
]

题解:

这题这102题(我上一篇博客)几乎是一模一样的,所有我只改动了一行代码, result.add(tempReslut)改为了result.add(0,tempReslut);

代码:

 public static List<List<Integer>> levelOrderBottom(TreeNode root) {
		 
		 List<List<Integer>> result=new ArrayList<List<Integer>>();//返回的最终结果
		   Queue<TreeNode> treeNodes2=new LinkedList<>();//用来存放每一层的节点
		   
		   //处理第一个节点(root)
		   if(root==null)
			   return result;
		   else {
			   List<Integer> temp=new ArrayList<>();//存放暂时的结果
			   temp.add(root.val);
			   treeNodes2.offer(root);
			   result.add(temp);
			  
		   }
		 
		   while(!treeNodes2.isEmpty())
		   {
			  int i=treeNodes2.size();
			  List<Integer> tempReslut=new ArrayList<>();//存放暂时的结果
			  while(i>0)//遍历这一层的所有节点
			  {
				  
				  TreeNode tNode=treeNodes2.poll();
				  if(tNode.left!=null)
				  {
					  tempReslut.add(tNode.left.val);
					  treeNodes2.offer(tNode.left);
				  }
				  if(tNode.right!=null)
				  {
					  tempReslut.add(tNode.right.val);
					  treeNodes2.offer(tNode.right);
				  }
				  i--;
			  }
			  if(!tempReslut.isEmpty()) result.add(0,tempReslut);  
			 
		   }
		   
		   return result;
	        
	    }


相关文章推荐

[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...

[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...

LeetCode 107. Binary Tree Level Order Traversal II

107. Binary Tree Level Order Traversal IIDescription Given a binary tree, return the bottom-up leve...

LeetCode(107)Binary Tree Level Order Traversal II

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left t...

LeetCode 107. Binary Tree Level Order Traversal II 树的BFS、DFS

Binary Tree Level Order Traversal II 题意 思路 代码 BFS DFS107. Binary Tree Level Order Traversal II Giv...

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...

leetcode解题之107 # Binary Tree Level Order Traversal II Java版

leetcode解题之107 # Binary Tree Level Order Traversal II Java版

leetcode-107-Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order trave...

【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

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 le...
  • fly_yr
  • fly_yr
  • 2015年10月14日 14:17
  • 351
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode107 Binary Tree Level Order Traversal II
举报原因:
原因补充:

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