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

Subject to the effect:

  Given a binary tree, traverse the level of its output result, which is represented by two-dimensional vector, each element of the vector for the current layer node set from left to right, upward from the leaf node and stored.

Understanding:

  Note that, when no one binary tree traversal, vector need to save these nodes, and secondly, these nodes in a two-dimensional vector from the leaf node is stored up, but traversing the binary tree from the top down.

  My approach is: first traversal of a binary tree of depth, to obtain the number of layers, namely the number of elements. When traversal, start from the root level, at the same time to begin a two-dimensional vector assignment from the last element.

      Used as an auxiliary queue, each layer traversed, the number of elements obtained when a layer, to facilitate the preservation of these elements with the vector. And the current non-empty child layer nodes all nodes to join the queue.

      When the queue is empty, i.e., iteration is complete.

  Can, without first obtaining binary tree depth. Direct access to each storage layer and, after traversing the complete binary tree, set against a two-dimensional vector can be.

Code C ++:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int getDepth(TreeNode* root){
        if(root==NULL) return 0;
        if(root->left==NULL && root->right==NULL) return 1;
        int left = getDepth(root->left);
        int right = getDepth(root->right);
        return left>right? left+1:right+1;
    }
    
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> vec;
        if(root==NULL) return vec;
        int depth = getDepth(root);      
        vec.resize(depth);
        
        vector<int> childVec;
        queue<TreeNode*> q;
        int childSize;
        TreeNode* node;
        q.push(root);
        while(q.empty()!=true){
            childSize = q.size();
            while(childSize>0){
                node = q.front();
                childVec.push_back(node->val);
                q.pop();
                if(node->left!=NULL)
                    q.push(node->left);
                if(node->right!=NULL)
                    q.push(node->right);
                --childSize;
            }
            vec[depth-1] = childVec;
            childVec.clear();
            depth--;
        }
        return vec;
    }
};

 

operation result:

  When executed with:  16 MS   memory consumption:  13.8 MB

Guess you like

Origin www.cnblogs.com/lpomeloz/p/10992957.html