DFS-栈

递归需要消耗额外的资源,这些资源是:
1.递归由于是函数调用自身,而函数调用是有时间和空间的消耗的:每一次函数调用,都需要在内存栈中分配空间以保存参数、返回地址以及临时变量,而往栈中压入数据和弹出数据都需要时间
2.递归中很多计算都是重复的,由于其本质是把一个问题分解成两个或者多个小问题,多个小问题存在相互重叠的部分,则存在重复计算,如fibonacci斐波那契数列的递归实现。->效率
3.调用栈可能会溢出,其实每一次函数调用会在内存栈中分配空间,而每个进程的栈的容量是有限的,当调用的层次太多时,就会超出栈的容量,从而导致栈溢出。->性能

DFS-Stack 步骤

init stack
stack.push(root)
visited(root)=true
while(stack.empty()):
    get top node
    stack.pop()
    for(n in node's neighbors):
        if(!visited(n)):
            stack.push(n)
            visited[n]=true

总体而言,点入栈方式看起来类似与BFS,但实际上,因为每次都是访问的栈顶元素,根据栈顶元素访问其邻居节点,岁所有,实际上元素的访问顺序是DFS,也就是先一条路走到黑,走不通之后pop掉一个点,接着访问栈顶元素的未被访问到的节点

Deepest Leaves Sum

Given a binary tree, return the sum of values of its deepest leaves.


image.png

image.png

这道题使用了递归和非递归2种方案,记录下叶子节点的值及其深度,最后再统一计算和

//递归
class Solution {
public:
    int deepestLeavesSum(TreeNode* root) {
        vector<vector<int>> max_depth_leaf;
        int sum = 0;
        if(!root)
            return sum;

        DFS(root,1,max_depth_leaf);
        
        int max_depth = 0;
        for(auto item:max_depth_leaf)
            max_depth = max(item[0],max_depth);
        for(auto item:max_depth_leaf){
            if(item[0]==max_depth)
                sum += item[1];
        }

        return sum;
    }

    void DFS(TreeNode *root,int depth,vector<vector<int>> &max_depth_leaf){
        if(!root) return;
        if(!root->left && !root->right)
            max_depth_leaf.push_back({depth,root->val});
        
        DFS(root->left,depth+1,max_depth_leaf);
        DFS(root->right,depth+1,max_depth_leaf);
    }
};


//非递归
class Solution {
public:
    int deepestLeavesSum(TreeNode* root) {
        stack<pair<TreeNode*,int>> my_stack;
        vector<vector<int>> max_depth_leaf;
        int sum = 0;
        
        if(!root)
            return sum;

        pair<TreeNode*,int> one_pair(root,1);

        my_stack.push(one_pair);

        TreeNode *tmp_node;
        int depth = 0;

        while(!my_stack.empty()){
            one_pair = my_stack.top();
            tmp_node = one_pair.first;
            depth = one_pair.second;
            my_stack.pop();

            if(!tmp_node->left && !tmp_node->right)
                max_depth_leaf.push_back({depth,tmp_node->val});

            if(tmp_node->left){
                one_pair.first = tmp_node->left;
                one_pair.second = depth+1;
                my_stack.push(one_pair);
            }
            
            if(tmp_node->right){
                one_pair.first = tmp_node->right;
                one_pair.second = depth+1;
                my_stack.push(one_pair);
            }
        }

        int max_depth = 0;
        for(auto item:max_depth_leaf)
            max_depth = max(item[0],max_depth);
        for(auto item:max_depth_leaf){
            if(item[0]==max_depth)
                sum += item[1];
        }

        return sum;
    }

  };
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 对树(tree)或者图(graph)而言,深度优先搜索(DFS) 和广度优先搜索(BFS)都是用于遍历或者搜...
    _Xie_阅读 1,078评论 0 2
  • 做题,实际写出例子,然后分析可能遇到的情况,慢慢的,思路就会出来了。 线性表 33. Search in Rota...
    小碧小琳阅读 1,637评论 0 2
  • 1)这本书为什么值得看: Python语言描述,如果学的Python用这本书学数据结构更合适 2016年出版,内容...
    孙怀阔阅读 12,693评论 0 15
  • 本文所使用的所有邻接表以及邻接矩阵定义均在之前博客,这里不再赘述 一、深度优先遍历DFS(Depth-first ...
    KM_0d16阅读 6,028评论 0 3
  • 今天去了晨光书院自闭症康复中心去考察可否建立学生党员活动基地。有一孩子忽然进屋四处看看,然后径直走到我面前直接坐到...
    棋萱阅读 142评论 0 1