LeetCode题解:Binary Tree Level Order Traversal I and II

Binary Tree Level Order Traversal

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

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

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

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

思路:

把栈式的访问改成队列式的访问。深度优先搜索改为广度优先搜索。

题解:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        if (!root)
            return vector<vector<int>>();
        
        queue<pair<TreeNode*, int>> visit_list;
        vector<vector<int>> ret;
        
        visit_list.push(make_pair(root, 0));
        while(!visit_list.empty())
        {
            while(visit_list.front().second >= ret.size())
                ret.push_back(vector<int>());
            ret[visit_list.front().second].push_back(visit_list.front().first->val);
            
            if (visit_list.front().first->left != nullptr)
                visit_list.push(make_pair(visit_list.front().first->left, visit_list.front().second + 1));
            if (visit_list.front().first->right != nullptr)
                visit_list.push(make_pair(visit_list.front().first->right, visit_list.front().second + 1));
            visit_list.pop();
        }
        
        return ret;
    }
};

思路:

同上,只不过多一次std::reverse的工作。注意这里用reverse并不会带来vector的值交换,而只是交换vector内部的指针而已,所以性能没有什么损失。

题解:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> retval;
        if (root == nullptr)
            return retval;
        
        queue<pair<TreeNode*, int>> traverse;
        traverse.push(make_pair(root, 1));
        while(!traverse.empty())
        {
            auto front = traverse.front();
            traverse.pop();
            
            if (retval.size() < front.second)
                retval.push_back(vector<int>());
            retval.back().push_back(front.first->val);
            
            if (front.first->left !=  nullptr)
                traverse.push(make_pair(front.first->left, front.second + 1));
            if (front.first->right != nullptr)
                traverse.push(make_pair(front.first->right, front.second + 1));
        }
        
        // since std::swap is used 
        // there is actually no vector swap, just pointers swap
        reverse(begin(retval), end(retval));
        
        return retval;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值