首页 > 其他分享 >404. 左叶子之和

404. 左叶子之和

时间:2023-04-06 20:46:03浏览次数:24  
标签:cur sum 叶子 404 result path root left

给定二叉树的根节点 root ,返回所有左叶子之和。

class Solution {
private:
    void sum_left(TreeNode *cur,vector<TreeNode*> &path,vector<int> &res){
        path.push_back(cur);
        if(cur->left == nullptr && cur->right == nullptr)
        {
            int len = path.size();
            if(len <= 1) return;
            TreeNode *node = path[path.size() - 2];
            if(cur == node->left)
                res.push_back(cur->val);
            return;
        } 
        if(cur->left){
            sum_left(cur->left,path,res);
            path.pop_back();
        }
        if (cur->right) { 
            sum_left(cur->right, path, res);
            path.pop_back(); 
        }
    }
public:
    int sumOfLeftLeaves(TreeNode* root) {
        vector<int> result;
        vector<TreeNode*> path;
        if (root == NULL) return 0;
        sum_left(root, path, result);
        int sum = 0;
        for(const auto &q:result)
        {
            sum += q;
        }
        return sum;
    }
    int sumOfLeftLeaves1(TreeNode* root) {
        vector<int> result;
        vector<TreeNode*> path;
        if (root == NULL) return 0;
        sum_left(root, path, result);
        int sum = 0;
        for(const auto &q:result)
        {
            sum += q;
        }
        return sum;
    }
    int sumOfLeftLeaves2(TreeNode* root) {
        stack<TreeNode*> st;
        if (root == NULL) return 0;
        st.push(root);
        int result = 0;
        while (!st.empty()) {
            TreeNode* node = st.top();
            st.pop();
            if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) {
                result += node->left->val;
            }
            if (node->right) st.push(node->right);
            if (node->left) st.push(node->left);
        }
        return result;
    }
};

标签:cur,sum,叶子,404,result,path,root,left
From: https://www.cnblogs.com/lihaoxiang/p/17294101.html

相关文章

  • vue3中路由错误自动跳转404页面 路由表写法
    定义路由表import{createRouter,createWebHashHistory}from"vue-router";constroutes=[ { path:"/", name:"home", component:Home, },//... { path:"/404", name:"404", component:()=&......
  • 230404
    Markdown快速入门(Typora)代码块include<iostream>usingnamespacestd;字体哈哈哈哈哈intmain(){while(0)}hhhhqqqqq作者:Ulysses插入一张图片00000111111https://github.com/......
  • 0404
         ......
  • java学习日记20230404-String类
    String类String对象用于保存字符串,也就是一组字符序列;字符串常量对象使用双引号包括起来的字符序列字符串的字符使用unicode字符编码,一个字符(不区分字母还是汉字)占用两个字节String常用的构造器:newString();newString(Stringoriginal);newString(char[]a);newString(char[]......
  • day17| 110.平衡二叉树;257.二叉树的所有路径;404.左叶子之和
    110.平衡二叉树 自顶向下递归 1.获得计算二叉树高度的函数2.对于遍历到的节点,首先计算左右子树的高度,看是否平衡3.在分别遍历到左右子树,判断左子树和右子树是否平衡 代码如下:classSolution:defisBalanced(self,root:TreeNode)->bool:defhei......
  • 门户发送请求出现404 Not Found
    一、问题背景在门户新部署了个微服务,利用nacos管理微服务media,门户测试出现404异常,后端工作日志也没有出现错误二、报错截图如下三、我的项目配置如下在项目配置bootstrap.yml#微服务配置spring:application:name:media-api#服务名media-api-dev.yamlcloud:......
  • 4404. X 进制减法
    原题链接代码#include<iostream>#include<algorithm>usingnamespacestd;constintN=100010;constintmod=1000000007;inta[N],b[N];//总结:记得开longlong/*题目中的65是指:1*1+2*2+3*2*10=65;两数相减取最小进制位,每一位的最小进制位为a和b......
  • 代码随想录Day17-Leetcode110.平衡二叉树,257. 二叉树的所有路径,404.左叶子之和
    110.平衡二叉树题目链接:https://leetcode.cn/problems/balanced-binary-tree/一个显然但似乎不太高效的方法是:通过递归获取左右子树高度,判断差;然后递归判断左右结点;那么一个显然的改进就是后序遍历/***Definitionforabinarytreenode.*functionTreeNode(val......
  • 有关wordpress文章页面出现404的问题
    有关wordpress文章页面出现404的问题修复的时候总结了一下原因:1.未开启apache的rewrite功能2..htaccess文件中的伪静态规则配置错误3.由于目录存在中文,编码问题导致解决方案:1.未开启apache的rewrite功能:使用命令sudoa2enmodrewrite开启mod_rewrite,然后修改配置文件......
  • Spring Mvc返回html页面404错误解决记录
        以前使用SpringMvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring-mvc.xml配置如下 :<beanid="viewResolver"class="org.springframework.web.servlet......