• Binary Tree Level Order Traversal I 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]
    ]

    思路:

    这两个题目都很基础,BFS搜索。

    题解:

    /**
     * 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) {
            vector<vector<int> > res;
            vector<int> tmp;
            queue<TreeNode *> q;
            int count = 0, num = 1;
            if(root==NULL)
                return res;
            q.push(root);
            while(!q.empty()) {
                for(int i=0;i<num;i++) {
                    root = q.front();
                    q.pop();
                    tmp.push_back(root->val);
                    if(root->left) {
                        count++;
                        q.push(root->left);
                    }
                    if(root->right) {
                        count++;
                        q.push(root->right);
                    }
                }
                res.push_back(tmp);
                num = count;
                count = 0;
                tmp.clear();
            }
            return res;
        }
    };

    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> > levelOrderBottom(TreeNode *root) {
            vector<vector<int> > res;
            vector<int> tmp;
            queue<TreeNode *> q;
            int count = 0, num = 1;
            if(root==NULL)
                return res;
            q.push(root);
            while(!q.empty()) {
                for(int i=0;i<num;i++) {
                    root = q.front();
                    q.pop();
                    tmp.push_back(root->val);
                    if(root->left) {
                        count++;
                        q.push(root->left);
                    }
                    if(root->right) {
                        count++;
                        q.push(root->right);
                    }
                }
                res.push_back(tmp);
                num = count;
                count = 0;
                tmp.clear();
            }
            reverse(res.begin(), res.end());
            return res;
        }
    };


  • 相关阅读:
    设计模式学习总结
    算法时间复杂度和空间复杂度表示
    SQLite简单使用
    接口,组合和继承的想法
    二叉树的学习
    Oracle 常用命令大汇总
    Oracle 最常用功能函数经典汇总
    oracle 常用command
    历史最牛演讲:Oracle总裁Yale演讲全文中英文对照
    深入abstract class和interface
  • 原文地址:https://www.cnblogs.com/jiasaidongqi/p/4165305.html
Copyright © 2020-2023  润新知