zoukankan      html  css  js  c++  java
  • 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;
        }
    };


  • 相关阅读:
    Linux下查看系统版本号信息的方法(转载)
    tomcat 启动超级慢
    新生代老年代GC组合
    GC 提前晋升
    Mysql 锁技术要点【转载】
    第39天:字符串连接、截取操作
    第38天:运算符、字符串对象常用方法
    第37天:小米手机图片展示
    第36天:倒计时:发动短信验证、跳转页面、关闭广告
    第35天:时钟效果
  • 原文地址:https://www.cnblogs.com/jiasaidongqi/p/4165305.html
Copyright © 2011-2022 走看看