Leetcode刷题笔记之 DFS&BFS

剑指 Offer 32 - I. 从上到下打印二叉树

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回:

[3,9,20,15,7]

提示:

  1. 节点总数 <= 1000

1。分层删除 耗时更短

   vector<int> levelOrder(TreeNode* root) {
        
        deque<TreeNode *> nodeQue;

        nodeQue.push_back(root);
        vector<int> res;
        if(root==NULL) return res;

        int n =1;
        while(!nodeQue.empty()){
            n=0;
            for(auto i : nodeQue){       
                res.push_back(i->val);
                if(i->left) {
                    nodeQue.push_back(i->left);
                    n++;
                }
                if(i->right) {
                    nodeQue.push_back(i->right);
                    n++;
                }
            }
           for(int i =nodeQue.size()-n;i>0;i--){
                nodeQue.pop_front();
            }
        }
        return res;
    }
  1. 逐个删除
    vector<int> levelOrder(TreeNode* root) {
        
        deque<TreeNode *> nodeQue;

        nodeQue.push_back(root);
        vector<int> res;
        if(root==NULL) return res;

        while(!nodeQue.empty()){
            TreeNode* node = nodeQue.front();
            nodeQue.pop_front();
            res.push_back(node->val);
            if(node->left)nodeQue.push_back(node->left);
            if(node->right)nodeQue.push_back(node->right);
        }
        return res;
    }

注意:一定要先判断左右节点是否为空后,再加入队列

剑指 Offer 32 - II. 从上到下打印二叉树 II

难度简单65

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

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

提示:

  1. 节点总数 <= 1000
vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        deque<TreeNode*> Que;
        if(root==NULL) return res;
        Que.push_back(root);
        
        while(!Que.empty()){
            vector<int> ares;
            int size = Que.size();
            for(auto i:Que){
                ares.push_back(i->val);
                if(i->left){
                    Que.push_back(i->left);                  
                }
                if(i->right){
                    Que.push_back(i->right);                   
                }
                
            }
            res.push_back(ares);
            for(int i=size;i>0;i--){
                Que.pop_front();
            }
        }
        return res;
    }

特别注意:for(auto i:Que)deque可以以这种方式进行遍历循环,queue不能使用这种方式进行遍历循环

简洁版

vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        deque<TreeNode*> Que;
        if(root==NULL) return res;
        Que.push_back(root);
        
        while(!Que.empty()){
            vector<int> ares;
            int size = Que.size();
            for(int n=0;n<size;n++){
                TreeNode* i = Que.front();
                Que.pop_front();
                ares.push_back(i->val);
                if(i->left){
                    Que.push_back(i->left);                  
                }
                if(i->right){
                    Que.push_back(i->right);                   
                }   
            }
            res.push_back(ares);
        }
        return res;
    }
剑指 Offer 34. 二叉树中和为某一值的路径

输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。

示例:
给定如下二叉树,以及目标和 sum = 22

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

返回:

[
   [5,4,11,2],
   [5,8,4,5]
]

提示:

  1. 节点总数 <= 10000

path作为迭代函数参数

class Solution {
public:
    vector<vector<int>> RES;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<int> res;
        pathSum_help( root, sum,res);
        return RES;
    }
    void pathSum_help(TreeNode* root, int n,vector<int> res){
        
        if(root==NULL)return;
        res.push_back(root->val);
        
        n = n-root->val;
        if(n==0&&root->left==NULL&&root->right==NULL){
            RES.push_back(res);
            return;
        }
        pathSum_help(root->left, n,res);
        pathSum_help(root->right, n,res);
    }
};

时间、空间复杂度较高,应当进行优化

path作为全局变量

class Solution {
public:
    vector<vector<int>> RES;
    vector<int> path;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<int> res;
        pathSum_help( root, sum);
        return RES;
    }
    void pathSum_help(TreeNode* root, int n){
        
        if(root==NULL)return;
        path.push_back(root->val);
        
        n = n-root->val;
        if(n==0&&root->left==NULL&&root->right==NULL){
            RES.push_back(path); //不能在这里直接返回,会导致path最后结点未删除
        }
        pathSum_help(root->left, n);
        pathSum_help(root->right, n);
        path.pop_back();
        return;
    }
};

注意,函数的返回位置应在,节点值加入path前或从path中删除后,否则导致节点值未从path中删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值