Tuesday, September 24, 2013

102. Binary Tree Level Order Traversal. (Medium)

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

 

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

 

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • -1000 <= Node.val <= 1000
A:
 ---------------简单的dfs, 每当看见一个节点,就加到结果上-----------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
helper(res, root, 0);
return res;
}

private:
void helper(vector<vector<int>>& res, TreeNode* root, int depth) {
if (!root)
return;
if (depth == res.size()) {
vector<int> a;
res.push_back(a);
}
res[depth].push_back(root->val);
helper(res, root->left, depth + 1);
helper(res, root->right, depth + 1);
}
};


----------------------------第二遍,-------每次取下来一层,   放到一个Node 的list里,  同时保留Integer的一层---------------------

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> all = new LinkedList();
        if(root == null)
            return all;
        List<TreeNode> lastLevel = new LinkedList();
        List<Integer> l = new LinkedList();
        lastLevel.add(root);
        l.add(root.val);
        all.add(l);
        while(lastLevel.isEmpty() ==false){
            l = new LinkedList();
            List<TreeNode> newLevel = new LinkedList();
            for(TreeNode node:lastLevel){
                if( node.left !=null ){
                    newLevel.add(node.left);
                    l.add(node.left.val);
                }
                if( node.right !=null){
                    newLevel.add(node.right);
                    l.add(node.right.val);
                }
            }
            lastLevel = newLevel;
            if(l.isEmpty()==false)
                all.add(l);
        }
        return all;
    }
}




Mistakes:
 1: 考虑到 stack的特性, 我们在加入的时候,要先push right child ,then left
 2  艹~~~~~~~~~  BFS 是用Queue, 而不是stack, TNND
 3:  每次要在while里重新new一个List, 单纯的clear()的话, ----由于其指针特性,会把all里的结果都clear掉
Learned:
1:   经常出现错误说“cannot create a generic array of .......”

Because of how generics in Java work, you cannot directly create an array of a generic type (such as Map<String, Object>[]). Instead, you create an array of the raw type (Map[]) and cast it to Map<String, Object>[]. This will cause an unavoidable (but suppressible) compiler warning.
This should work for what you need:
Map<String, Object>[] myArray = (Map<String, Object>[]) new Map[10];
You may want to annotate the method this occurs in with @SupressWarnings("unchecked"), to prevent the warning from being shown.

还有一个办法,就是  在new 的时候,不加generic type, 例如: 直接也不进行上面的cast
        Stack<TreeNode>[] twoStack = new Stack[2];

3: Queue is an Interface not a class. ,  so,we cannot use them as class.
Instead, we need use LinkedList or PriorityQueue and etc to simulate it.

No comments:

Post a Comment