Leetcode 366. Find Leaves of Binary Tree

本文探讨了三种不同的算法策略:暴力法、两次遍历法和一次遍历法,来寻找二叉树中所有叶子节点的路径。暴力法的时间复杂度为O(D*N),两次遍历法为O(2N),而一次遍历法达到最优化,时间复杂度为O(N)。通过对比分析,展示了高效解决树形问题的不同方法。
摘要由CSDN通过智能技术生成

题目

在这里插入图片描述

解法1:暴力

按照题目的描述,不断的去收集叶节点,然后断开这些叶节点

class Solution {
public:
    void helper(TreeNode* root,TreeNode* parent, vector<int>& tmp, string child){
        if(!root) return;
        if(!root->left && !root->right){
            tmp.push_back(root->val);
            if(child == "left") parent->left = nullptr;
            if(child == "right") parent->right = nullptr;
            return;
        }
        helper(root->left,root,tmp,"left");
        helper(root->right,root,tmp,"right");
        return;
    }
    vector<vector<int>> findLeaves(TreeNode* root) {
        if(!root) return {};
        if(!root->left && !root->right) return {{root->val}};
        vector<vector<int>> ans;
        while(true){
            if(!root->left && !root->right) {
                ans.push_back({root->val});
                return ans;
            }
            vector<int> tmp;
            TreeNode* parent = new TreeNode();
            helper(root,parent,tmp,"");
            ans.push_back(tmp);
        }
        return ans;
    }
};

时间复杂度:O(D*N),D为树的的深度,N为节点个数,这个helper函数需要访问D次,每次访问所有节点
空间复杂度:O(D),递归带来的

解法2: two pass

核心思想是,从叶节点开始往上数,到当前节点的深度(是倒过来的深度),决定了这个节点在答案中的位置
首先获得深度,预定义好答案的长度,然后更求深度一样的在每个位置加入答案

class Solution {
public:
    int getDepth(TreeNode* root){
        if(!root) return 0;
        return max(getDepth(root->left),getDepth(root->right)) + 1;
    }
    int helper(TreeNode* root,vector<vector<int>>& ans){
        if(!root) return -1;
        int ind = max(helper(root->left,ans),helper(root->right,ans)) + 1;
        ans[ind].push_back(root->val);
        return ind;
    }
    vector<vector<int>> findLeaves(TreeNode* root) {
        int depth = getDepth(root);
        // cout << depth << endl;
        vector<vector<int>> ans(depth);
        helper(root,ans);
        return ans;
    }
};

时间复杂度:O(2N)

解法3:one pass

无需预先求的深度,如果当前节点获得的深度已经大于答案的长度,就增加答案的长度即可

class Solution {
public:
    int helper(TreeNode* root,vector<vector<int>>& ans){
        if(!root) return -1;
        int ind = max(helper(root->left,ans),helper(root->right,ans)) + 1;
        if(ind >= ans.size()){
            ans.push_back({root->val});
        }else{
            ans[ind].push_back(root->val);
        }
        
        return ind;
    }
    vector<vector<int>> findLeaves(TreeNode* root) {
        vector<vector<int>> ans;
        helper(root,ans);
        return ans;
    }
};

时间复杂度:O(N)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值