LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal

Title:

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

思路:使用队列,并且每次记录下每层的数目,这样从队列中取元素也就清楚了每层的元素
/**
 * 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:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> Q;
        vector<vector<int> > results;
        if (!root)
            return results;
        vector<int> result;
        Q.push(root);
        int count_pre = 1;
        int count_cur = 0;
        while (!Q.empty()){
            TreeNode* top = Q.front();
            result.push_back(top->val);
            Q.pop();
            count_pre--;
            if (top->left){
                Q.push(top->left);
                count_cur++;
            }
            if (top->right){
                Q.push(top->right);
                count_cur++;
            }
            if (count_pre == 0){
                count_pre = count_cur;
                count_cur = 0;
                results.push_back(result);
                result.clear();
            }
        }
        return results;
    }
};

还可以使用递归。关键是要保存到对应的层数

/**
 * 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:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int> > result;
        travel(root,result,1);
        return result;
    }
    void travel(TreeNode* root,vector<vector<int> > &result,int level){
        if (!root)
            return ;
        if (level > result.size())
            result.push_back(vector<int> ());
        result[level-1].push_back(root->val);
        travel(root->left,result,level+1);
        travel(root->right,result,level+1);
    }
};

Title:

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,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]一样的思路啊!如果不使用递归,则使用一个标记,看这层是否反转,直接使用reverse函数即可。如果使用递归,则也是在相应的层反转。但是因为是在插入的过程中,所以应该是插入每层的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 {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int> > results;
        travel(root,1,results,true);
        return results;
    }
    void travel(TreeNode* root, int level, vector<vector<int> > &results, bool left_to_right){
        if (!root)
            return;
        if (level > results.size())
            results.push_back(vector<int>());
        if (left_to_right)
            results[level-1].push_back(root->val);
        else
            results[level-1].insert(results[level-1].begin(),root->val);
        travel(root->left,level+1,results,!left_to_right);
        travel(root->right,level+1,results,!left_to_right);
    }
};
/**
 * 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:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int> > results;
        travel(root,1,results,true);
        return results;
    }
    void travel(TreeNode* root, int level, vector<vector<int> > &results, bool left_to_right){
        if (!root)
            return;
        if (level > results.size())
            results.push_back(vector<int>());
        if (left_to_right)
            results[level-1].push_back(root->val);
        else
            results[level-1].insert(results[level-1].begin(),root->val);
        travel(root->left,level+1,results,!left_to_right);
        travel(root->right,level+1,results,!left_to_right);
    }
};
时间: 2024-10-11 20:08:43

LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal的相关文章

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal 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,#

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: 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

[LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values

Binary Tree ZigZag Level Order Traversal leetcode java

题目: 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,#,#,15,7}, 3 / 9 20 / 15 7 return its

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri