首页 > 编程语言 >代码随想录算法训练营第第17天 | 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和

代码随想录算法训练营第第17天 | 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和

时间:2024-05-24 23:18:44浏览次数:11  
标签:node right return val 17 随想录 二叉树 root left

三道题都没想出来,还是要加强递归的练习
110.平衡二叉树 (优先掌握递归)

再一次涉及到,什么是高度,什么是深度,可以巩固一下。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0110.平衡二叉树.html

function getHeight(node) {
    if (node === null) return 0;
    let leftHeight = 0;
    let rightHeight = 0;
    leftHeight = getHeight(node.left);
    if (leftHeight === -1) {
        return -1
    }

    rightHeight = getHeight(node.right);
    if (rightHeight === -1) {
        return -1;
    }
    if (Math.abs(leftHeight - rightHeight)>1) {
        return -1;
    }
    return Math.max(leftHeight,rightHeight) + 1;
}

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
    let height = getHeight(root);
    if (height === -1) {
        return false;
    }
    return true;
};

  1. 二叉树的所有路径 (优先掌握递归)

这是大家第一次接触到回溯的过程, 我在视频里重点讲解了 本题为什么要有回溯,已经回溯的过程。
如果对回溯 似懂非懂,没关系, 可以先有个印象。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0257.二叉树的所有路径.html

递归加回溯
function getAllPath(node, path, results) {
    path.push(node.val);
    if (node.left === null && node.right === null) {
        let sPath = '';
        for (let i=0;i<path.length-1; i++) {
            sPath += path[i];
            sPath += '->';
        }
        sPath += path[path.length - 1];
        results.push(sPath);
        return;
    }
    
    if (node.left) {
        getAllPath(node.left, path, results);
        path.pop();
    }
    if (node.right) {
        getAllPath(node.right, path, results);
        path.pop();
    }
    
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function(root) {
    const path = [];
    const results = [];
    getAllPath(root, path, results);
    return results;
};

404.左叶子之和 (优先掌握递归)

其实本题有点文字游戏,搞清楚什么是左叶子,剩下的就是二叉树的基本操作。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0404.左叶子之和.html

递归
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumOfLeftLeaves = function(root) {
    if (root == null) return 0;
    if (root.left === null && root.right === null) return 0;
    let leftTol = sumOfLeftLeaves(root.left);
    if (root.left && root.left.left === null && root.left.right === null) {
        leftTol = root.left.val;
    }
    leftTol += sumOfLeftLeaves(root.right);
    return leftTol;

};

标签:node,right,return,val,17,随想录,二叉树,root,left
From: https://www.cnblogs.com/yuanyf6/p/18211820

相关文章

  • P8655 [蓝桥杯 2017 国 B] 发现环
    原题链接题解有点像拓扑排序拓扑排序怎么做来着?首先找老祖节点对不对?老祖节点有什么特性?入度为零而在无向图中,我们把叶子节点看成老祖节点,它们有什么特性?连接的边只有一条code#include<bits/stdc++.h>usingnamespacestd;vector<int>G[100005];intcon[100005]={......
  • 算法随想录打卡第三天|链表
    //203移除链表元素publicListNoderemoveElements(ListNodehead,intval){//创建虚拟头指针不对//ListNoderes=head;//创建一个虚拟头结点ListNoderes=newListNode(val-1);res.next=head;ListNodeprev=res;......
  • 二叉树搜索树 洛谷P5076
    洛谷P1827#include<bits/stdc++.h>usingnamespacestd;intn,root,cnt,opt,x;structnode{intvalue,left,right,size,num;node(intl,intr,ints,intv):left(l),right(r),size(s),value(v),num(1){}node(){}}t[10010];voidu......
  • 代码随想录算法训练营第一天 | 704.二分查找;27. 移除元素
    代码随想录算法训练营第一天|704.二分查找(红蓝模板法);27.移除元素(双指针法)704题链接:https://leetcode.cn/problems/binary-search/description/二分查找:https://programmercarl.com/0704.二分查找.html#其他语言版本二分查找红蓝法笔记:二分查找为什么总是写错?_哔哩哔哩_bil......
  • 代码随想录算法训练营第十五天 | 层序遍历 、226.翻转二叉树、101.对称二叉树
    层序遍历题目链接:学会二叉树的层序遍历,可以一口气打完以下十题:102.二叉树的层序遍历107.二叉树的层次遍历II199.二叉树的右视图637.二叉树的层平均值429.N叉树的层序遍历515.在每个树行中找最大值116.填充每个节点的下一个右侧节点指针117.填充每个节点的下一个右侧节点......
  • 算法打卡 Day18(二叉树)-层序遍历 + 翻转二叉树 + 对称二叉树
    文章目录层序遍历相关题目Leetcode226-翻转二叉树题目描述解题思路Leetcode101-对称二叉树题目描述解题思路层序遍历我们使用队列模拟二叉树的层序遍历。相关题目102.二叉树的层序遍历classSolution{public:vector<vector<int>>levelOrder(TreeNode......
  • 算法打卡 Day14(二叉树)-理论基础 + 递归遍历 + 迭代遍历 + 统一迭代
    文章目录理论基础满二叉树完全二叉树二叉搜索树平衡二叉搜索树二叉树的存储方式链式存储顺序存储二叉树的遍历方式二叉树的定义递归遍历leetcode144-二叉树的前序遍历leetcode145-二叉树的后序遍历leetcode94-二叉树的中序遍历迭代遍历前序遍历后序遍历中序遍历统......
  • 代码随想录算法训练营第三十六天|860.柠檬水找零、406.根据身高重建队列、452. 用最少
    860.柠檬水找零文档讲解:代码随想录题目链接:.-力扣(LeetCode)注意看提示:bills[i] 不是 5 就是 10 或是 20 场景较为固定遇到了20,优先消耗10classSolution:deflemonadeChange(self,bills:List[int])->bool:total={5:0,10:0,20:0}......
  • P3817 小A的糖果
    题目链接#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+7;intn,x;inta[N];longlongsum;intmain(){cin>>n>>x;for(inti=1;i<=n;i++)cin>>a[i];if(a[1]>x){//当第一个数大于已经超过xsum......
  • [Usaco2017 Open]Bovine Genomics 题解^&*^(
    不知道为啥,我死活想不到二分(楼下正解)所以,就有了这篇题解可以看到,这道题离暴力的距离只有一步!就是数组开不下!!小问答:数组开不下时,你会?A:mapB:优化代码C:gp_hash_table由于正在学hash,所以容易想到...tong[本来的下标%9999999]然后就玄学的过了。。。ACcode#include<bi......