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

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.youkuaiyun.com/crazy__chen/article/details/46378669

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值