LeetCode 107. Binary Tree Level Order Traversal II

原题链接在这里: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,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

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

题解:

Binary Tree Level Order Traversal相似,只是返过来加链表。每次把item从头加进res中.

Time Complexity: O(n). Space O(n).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public List<List<Integer>> levelOrderBottom(TreeNode root) {
18         LinkedList<List<Integer>> res = new LinkedList<>();
19         if(root == null){
20             return res;
21         }
22 
23         LinkedList<TreeNode> que = new LinkedList<>();
24         que.add(root);
25         while(!que.isEmpty()){
26             int size = que.size();
27             List<Integer> item = new ArrayList<>();
28             while(size-- > 0){
29                 TreeNode cur = que.poll();
30                 item.add(cur.val);
31                 if(cur.left != null){
32                     que.add(cur.left);
33                 }
34 
35                 if(cur.right != null){
36                     que.add(cur.right);
37                 }
38             }
39 
40             res.addFirst(item);
41         }
42 
43         return res;
44     }
45 }

AC C++:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     vector<vector<int>> levelOrderBottom(TreeNode* root) {
15         if(!root){
16             return {};
17         }
18 
19         vector<vector<int>> res;
20         deque<TreeNode*> que;
21         que.push_back(root);
22 
23         while(!que.empty()){
24             int size = que.size();
25             vector<int> item;
26             while(size-- > 0){
27                 TreeNode* cur = que.front();
28                 que.pop_front();
29                 item.push_back(cur->val);
30 
31                 if(cur->left){
32                     que.push_back(cur->left);
33                 }
34 
35                 if(cur->right){
36                     que.push_back(cur->right);
37                 }
38             }
39 
40             res.push_back(item);
41         }
42 
43         reverse(res.begin(), res.end());
44         return res;
45     }
46 };

AC Python:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
 9         if not root:
10             return []
11         
12         res = deque()
13         que = deque([root])
14 
15         while que:
16             size = len(que)
17             item = []
18 
19             for _ in range(size):
20                 cur = que.popleft()
21                 item.append(cur.val)
22 
23                 if cur.left:
24                     que.append(cur.left)
25 
26                 if cur.right:
27                     que.append(cur.right)
28                 
29             res.appendleft(item)
30         
31         return list(res)
32         

 

posted @ 2015-09-05 01:47  Dylan_Java_NYC  阅读(168)  评论(0编辑  收藏  举报