leetcode102. Binary Tree Level Order Traversal & 103. Zigzag Level

这两道题同样是树的层级遍历。所以,特地将这两道题拿在一起说
先来说102:
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,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
这道题的题意是将树进行分层遍历,并把每层的值都存在数组中。很明显,题目的意思要分层遍历,有两种思路。
思路一:递归对树进行深度搜索并记录每一层的深度,在递归的时候将每一层的树的结点的值存入,这个思路也比较直观,要注意的地方就是二维vector的行数等于递归的深度的时候才需要新建一层vector,其他时候并不需要。代码如下:

/**
 * 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 {
private:
    void Order(TreeNode* root,vector<vector<int>> &order,int count)
    {
        if(!root) return;
        if(order.size()==count)
            order.push_back(vector<int>());
        order[count].push_back(root->val);
        Order(root->left,order,count+1);
        Order(root->right,order,count+1);
    }
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        Order(root,res,0);
        return res;
    }
};

思路二:也是针对这类分层输出或者后面提到的Zigzag level的问题,使用的是广度优先搜索,用队列来实现将每一层的结点存入,然后正序输出。这部分广度优先搜索的代码会在103问放出,因为两道题思路都差不多,所以这里就不贴了。

Leetcode 103. Zigzag Level
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
这道题要求我们通过折线的方式进行遍历。一样的,根据前一题的两种思路引伸到这题同样有两种思路:
思路一:在第一问分层偏离的基础上,将第一问得到的结果的奇数行的vector,调用其reveser函数进行反转。
思路二:利用队列进行广度优先搜索,思路就非常明显了。就是在每层的队列中反方向幅值给vector temp就行。具体到代码就是:

偶数行:temp[I]=queue.front()
奇数行:temp[queue.size()-1-i]=queue.front()

利用这种相反赋值实现反转,这种情况就不能利用在深度搜索上,因为深度搜索会对子树的左右子结点反转,不会对整个树的每一层进行反转,只能在最后得到结果后反转。
结果代码如下:

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    vector<vector<int>> res;
    if(!root)
        return res;
    queue<TreeNode*> treequeue;
    treequeue.push(root);
    //判断是从右到左还是从左到右
    bool lefttoright=true;
    while(!treequeue.empty())
    {
        int size=treequeue.size();
        vector<int> temp(size);
        for(int i=0;i<size;++i)
        {
            TreeNode* node=treequeue.front();
            if(lefttoright)
                temp[i]=node->val;
            else
                temp[size-1-i]=node->val;
            treequeue.pop();
            if(node->left)
                treequeue.push(node->left);
            if(node->right)
                treequeue.push(node->right);
        }
        lefttoright=!lefttoright;
        res.push_back(temp);
    }
    return res;    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值