头图

Zero title: Algorithm (leetcode, with mind map + all solutions) 300 questions (124) Maximum path sum in binary tree

a topic description

题目描述
题目描述

Two solutions overview (mind map)

思维导图

All three solutions

Interviewer: The topic is almost the same, how is it? Do you have any ideas?

Madman Zhang San: For the binary tree problem, we can give priority to using [recursion] .

Interviewer: Oh? Why is this?

Madman Zhang San: There is a book on biology - "structure and function adapt" , then I think there are similar things in "algorithm", such as "data structure and algorithm adapt" - carefully observe, you will find , the structure of the current root node and its left and right nodes is "isomorphic" , then we will naturally think of using "recursion" .

Interviewer: 算法通关秘籍

Madman Zhang San: 洒洒水啦~

Interviewer (with a serious face): Talk is cheap, show me the code.

Narrator: After 4 minutes and 41 seconds, Zhang San wrote the following code in unison.

1 Scenario 1

1) Code:

 // 方案1 “就地更新 - 再次遍历法(自己)”。

// 技巧:二叉树的题目应优先考虑使用递归。

// 思路:
// 1)状态初始化:resVal = Number.NEGATIVE_INFINITY (因 要求最大值,故 先置为 最大的负数值 )
// 2)核心1:根据情况,更新 树上各节点的值 。
// 3)核心2:遍历新树,更新最大值 resVal 。
// 4)返回结果 resVal 。
var maxPathSum = function(root) {
    // 根据情况,更新 树上各节点的值 。
    const updateTree = (root = null) => {
        // 1.1)递归出口1
        if (!root) {
            return;
        }

        // 1.2)递归出口2
        let {left, right, val} = root;
        if ((!left) && (!right)) {
            return;
        }

        // 2)递归主体
        // 2.1)更新 左子树的值 。
        updateTree(left);
        // 2.2)更新 右子树的值 。
        updateTree(right);
        
        // 2.3)更新 根节点的值 。
        // 分3种情况:带上左子树的值 或 带上右子树的值 或 左、右子树值均不带。
        root.val = val + Math.max((left?.val || 0), (right?.val || 0), 0);

        // 2.4)更新结果值 resVal 。
        resVal = Math.max(resVal, val + (left?.val || 0) + (right?.val || 0));
    };

    // 遍历新树,更新最大值 resVal 。
    const getMaxValByNewTree = (root = null) => {
        // 1)递归出口
        if (!root) {
            return;
        }

        // 2)递归主体
        const {left, right, val} = root;
        // 2.1)根节点
        resVal = Math.max(resVal, val);
        // 2.2)左子树
        getMaxValByNewTree(left);
        // 2.3)右子树
        getMaxValByNewTree(right);
    };

    // 1)状态初始化:resVal = Number.NEGATIVE_INFINITY (因 要求最大值,故 先置为 最大的负数值 )
    let resVal = Number.NEGATIVE_INFINITY;

    // 2)核心1:根据情况,更新 树上各节点的值 。
    updateTree(root);

    // 3)核心2:遍历新树,更新最大值 resVal 。
    getMaxValByNewTree(root);

    // 4)返回结果 resVal 。
    return resVal;
};

Madman Zhang San: How is it?

Interviewer: A look of disdain.
我问你,这解法保熟(是最优解)吗?

Madman Zhang San: Full of confidence.
我一个正经的算法人,还能给你写生瓜(非最优解)算法不成?

Interviewer: Take a closer look at your solution, have you traversed it twice? So what did they do in these two traversals? Can we just do 1 pass - ie do a merge of the passes?

Narrator: Facing the interviewer's one-button 3-question ("implying one-button three-link??"), Zhang directly "scared three drops of cold sweat".

Madman Zhang San (his face turned pale, and read the code carefully. Suddenly he realized):

 1、确实遍历了2次。
2、分了做了如下工作:
1)updateTree:遍历树,当前节点值从左、右子树的值和0中选出最大值,并加到当前根节点的值上。并 更新结果值 resVal (因为此时的 resVal 有可能是 有当前根节点、当前左、右子树一起组成的路径值之和)。
2)getMaxValByNewTree:更新结果值 resVal(因为此时的 resVal 有可能是在当前根节点取得的)。
3、看起来,updateTree、getMaxValByNewTree中产生的操作可以合并到一起。


Beside: After a while (4 minutes and 11 seconds passed), Zhang San wrote the following code based on scheme 1 and new ideas.

2 Option 2

1) Code:

 // 方案2 “官方,递归法。(本质:跟方案1的思路是一致的,只是少了1次树的遍历 —— getMaxValByNewTree)”。
// 参考:
// 1)https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/solution/er-cha-shu-zhong-de-zui-da-lu-jing-he-by-leetcode-/

// 思路:
// 1)状态初始化:resVal = Number.NEGATIVE_INFINITY 
// (因 要求最大值,故 先置为 最大的负数值 )。
// 2)核心:遍历新树,更新最大值 resVal 。
// 3)返回结果 resVal 。
var maxPathSum = function(root) {
    // 核心:递归处理!
    const maxGain = (root = null) => {
        // 1)递归出口
        if (!root) {
            return 0;
        }

        // 2)递归主体
        // 2.1)递归计算左右子节点的最大贡献值
        // 只有在最大贡献值大于 0 时,才会选取对应子节点
        const leftGain = Math.max(maxGain(root.left), 0),
            rightGain = Math.max(maxGain(root.right), 0),
            // 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值
            priceNewpath = root.val + leftGain + rightGain;
        
        // 2.2)更新结果值 resVal 。
        resVal = Math.max(resVal, priceNewpath);

        // 2.3)返回当前根节点的最大贡献值。
        return root.val + Math.max(leftGain, rightGain);
    }

    // 1)状态初始化:resVal = Number.NEGATIVE_INFINITY 
    // (因 要求最大值,故 先置为 最大的负数值 )。
    let resVal = Number.NEGATIVE_INFINITY;
    
    // 2)核心:遍历新树,更新最大值 resVal 。
    maxGain(root);

    // 3)返回结果 resVal 。
    return resVal;
}

Madman Zhang San (after writing the above code, he hurriedly asked the interviewer): This is the best solution, so it is considered to pass the interview, right?

Interviewer: Ruzi can be taught~
哎,你他娘的还真是个天才

Madman Zhang San:
算法人(打工人)的面试往往就是这么朴实无华且枯燥
Another offer, and I'm going to be the CEO soon. Should I go to eat Shaxian snacks in the evening? Or Lanzhou Ramen ? Alas, having too many choices is also an annoyance!

Four resource sharing & more

1 Historical Articles - Overview

Article name solution reading volume
1. Two Sum 3 types in total 2.7k+
2. Add Two Numbers 4 types in total 2.7k+
3. Longest Substring Without Repeating Characters 3 types in total 2.6k+
4. Find the Median of Two Sorted Arrays 3 types in total 2.8k+
5. Longest Palindromic Substring 4 types in total 2.8k+
6. ZigZag Conversion 2 types in total 1.9k+
7. Reverse Integer 2 types in total 2.4k+
8. String to Integer (atoi) 3 types in total 4.2k+
9. Palindrome Number 3 types in total 4.3k+
11. Container With Most Water 5 in total 4.0k+
12. Integer to Roman 3 types in total 3.2k+
13. Roman to Integer 3 types in total 3.8k+
14. Longest Common Prefix 4 types in total 3.0k+
15. The Sum of Three Numbers (3Sum) 3 types in total 60.7k+
16. 3Sum Closest 3 types in total 4.7k+
17. Letter Combinations of a Phone Number 3 types in total 3.1k+
18. The sum of four numbers (4Sum) 4 types in total 11.5k+
19. Remove Nth Node From End of List 4 types in total 1.2k+
20. Valid Parentheses 2 types in total 1.8k+
21. Merge Two Sorted Lists 3 types in total 1.2k+
22. Generate Parentheses 4 types in total 1.1k+
23. Merge k Sorted Lists 4 types in total 0.9k+
24. Swap Nodes in Pairs 3 types in total 0.5k+
25. Reverse Nodes in k-Group 5 in total 1.3k+
26. Remove Duplicates from Sorted Array 4 types in total 1.3k+
27. Remove Element 4 types in total 0.4k+
28. Implement strStr() (Implement strStr()) 5 in total 0.8k+
29. Divide Two Integers 4 types in total 0.6k+
30. Substring with Concatenation of All Words 3 types in total 0.6k+
31. Next Permutation 2 types in total 0.8k+
32. Longest Valid Parentheses 2 types in total 1.4k+
33. Search in Rotated Sorted Array 3 types in total 1.0k+
34. Find First and Last Position of Element in Sorted Array 3 types in total 0.5k+
35. Search Insert Position 3 types in total 0.3k+
36. Valid Sudoku 1 in total 0.6k+
38. Count and Say 5 in total 1.1k+
39. Combination Sum 3 types in total 1.4k+
40. Combination Sum II 2 types in total 1.6k+
41. First Missing Positive 3 types in total 1.2k+
53. Maximum Subarray Sum (Maximum Subarray) 3 types in total 0.3k+
88. Merge Sorted Array 3 types in total 0.4k+
102. Binary Tree Level Order Traversal 3 types in total 0.4k+
146. LRU Cache (LRU Cache) 2 types in total 0.5k+
160. Intersection of Two Linked Lists 2 types in total 0.1k+
200. Number of Islands 4 types in total 0.1k+
206. Reverse Linked List 3 types in total 1.0k+
215. Kth Largest Element in an Array 3 types in total 0.5k+
236. Lowest Common Ancestor of a Binary Tree 3 types in total 0.1k+
2119. A Number After a Double Reversal 2 types in total 0.3k+
2120. Execution of All Suffix Instructions Staying in a Grid 1 in total 0.4k+
2124. Check if All A's Appears Before All B's 4 types in total 0.4k+
2125. Number of Laser Beams in a Bank 3 types in total 0.3k+
2126. Destroying Asteroids 2 types in total 1.6k+
2129. Capitalize the Title 2 types in total 0.6k+
2130. Maximum Twin Sum of a Linked List 2 types in total 0.6k+
2133. Check if Every Row and Column Contains All Numbers 1 in total 0.6k+

刷题进度 - LeetCode:574 / 2720 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝