leetcode--Binary Tree Level Order Traversal II

来源:互联网 发布:卡五星源码 编辑:程序博客网 时间:2024/06/03 19:05

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


题意:从下往上,将每一层从左往右存储

分类:二叉树


解法1:层次遍历,使用队列进行层次遍历,关键在于标记每一层结束的位置。使用了ceng来标记,每次low==ceng,说明该层遍历完毕,将ceng更新为当前high

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<List<Integer>> levelOrderBottom(TreeNode root) {  
  12.         Stack<TreeNode> stack = new Stack<TreeNode>();    
  13.         LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();    
  14.         if(root == nullreturn res;    
  15.         List<Integer> t = new ArrayList<Integer>();    
  16.         int low = 0;    
  17.         int high = 0;    
  18.         int ceng = 0;    
  19.         stack.add(root);    
  20.         t.add(root.val);    
  21.         res.add(t);    
  22.         t = new ArrayList<Integer>();;    
  23.         while(low<=high){                
  24.             TreeNode cur = stack.get(low);              
  25.             if(cur.left!=null){    
  26.                 stack.add(cur.left);    
  27.                 t.add(cur.left.val);    
  28.                 high++;    
  29.             }    
  30.             if(cur.right!=null){    
  31.                 stack.add(cur.right);    
  32.                 t.add(cur.right.val);    
  33.                 high++;    
  34.             }    
  35.             if(low==ceng){    
  36.                 if(t.size()!=0) res.addFirst(t);;    
  37.                 t = new ArrayList<Integer>();    
  38.                 ceng = high;    
  39.             }    
  40.             low++;    
  41.         }    
  42.         return res;    
  43.     }  
  44. }  


解法2:层次遍历,而辨别层结束的方法是,在每层结尾,都添加一个标记元素,遍历时遇到标记元素,说明该层遍历结束

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<List<Integer>> levelOrderBottom(TreeNode root) {  
  12.         LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();  
  13.         Queue<TreeNode> queue = new ArrayDeque<TreeNode>();  
  14.         List<Integer> item = new ArrayList<Integer>();  
  15.         if(root==nullreturn res;  
  16.         queue.add(root);  
  17.         TreeNode mark = new TreeNode(-1);//哨兵元素,用于标记某层的结束  
  18.         queue.add(mark);  
  19.         while(queue.size()>1){//因为有哨兵,所以至少是1,然而这样最后一层会没有被加入res  
  20.             TreeNode cur = queue.poll();  
  21.             if(cur!=mark){  
  22.                 item.add(cur.val);  
  23.                 if(cur.left!=null) queue.add(cur.left);  
  24.                 if(cur.right!=null) queue.add(cur.right);                      
  25.             }else{//当前为哨兵元素  
  26.                 res.addFirst(new ArrayList<Integer>(item));  
  27.                 item.clear();  
  28.                 queue.add(mark);  
  29.             }  
  30.         }  
  31.         res.addFirst(item);//最后一层  
  32.         return res;  
  33.     }  
  34. }  


原文链接http://blog.csdn.net/crazy__chen/article/details/46378669

原创粉丝点击