当前位置:网站首页>[leetcode] notes on recursive time value transfer and reference transfer
[leetcode] notes on recursive time value transfer and reference transfer
2022-06-11 01:54:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of binary tree 】
stay java The basic types in are value passing , Instead of basic types, they are passed by reference ; When it is used as a parameter in the recursive process , Pay attention to branch pollution , And whether it is necessary to go back
int Pass as a parameter
because int It's worth passing on , Every time a method is called recursively, a value is passed in , After recursive call , It does not affect the original value , That is, there is no branch pollution problem
Such as :LeetCode104 Find the sum of numbers from root node to leaf node
int ans = 0; public int sumNumbers(TreeNode root) { getSum(root, 0); return ans; } private void getSum(TreeNode root, int sum) { if (root == null) return; sum = sum * 10 + root.val; if (root.left == null && root.right == null) { ans+=sum; } getSum(root.left, sum); getSum(root.right, sum); }String Pass as a parameter
String Itself is passed by reference , But he is special ,String yes final Of , Every time + Or the assignment actually generates a new String object , That is, there is no branch pollution problem in the recursive process , No backtracking operation is required ;
With the above int Different : If there is an operation , Each recursive call generates a new String Into
Such as :LeetCode257 All paths of binary tree
public List<String> binaryTreePaths(TreeNode root) { List<String> ans = new ArrayList<>(); binaryTreePaths2(root, "", ans); return ans; } // string Adding is too slow // Just want to explain , Use it directly String Why not go back , Because use String Of + A new object has been generated by // The next time a value is passed recursively, a new object is passed in private void binaryTreePaths(TreeNode root, String res, List<String> ans) { if (root == null) return; if (res.length() == 0) { res += root.val; } else { res = res + "->" + root.val; } if (root.left == null && root.right == null) { ans.add(res); } binaryTreePaths(root.left, res, ans); binaryTreePaths(root.right, res, ans); }List Pass in as a parameter
list Is the easiest point to make mistakes , Sometimes it is easy to overlook the fact that list After being passed in as a parameter , Then make corresponding modifications , This of the original method list The changes have been synchronized without realizing .
The same is true in recursion , When calling this method recursively list Made modifications , That goes back to the original when using the list The modification will be kept synchronously , That is, branch pollution in recursion , Scenarios that require backtracking
Such as :LeetCode113 The sum of the paths II
List<List<Integer>> ans = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int targetSum) { LinkedList<Integer> res = new LinkedList<>(); pathSum(root, targetSum, res); return ans; } // Depth-first search + to flash back private void pathSum(TreeNode root, int targetSum, LinkedList<Integer> res) { if (root == null) return; res.add(root.val); if (root.left == null && root.right == null) { // The leaf node satisfies the final condition if (root.val == targetSum) { // want new A collection goes in , Otherwise, it will be synchronously modified later ans.add(new LinkedList<>(res)); } } pathSum(root.left, targetSum - root.val, res); pathSum(root.right, targetSum - root.val, res); // Go back , Remove the current node from the collection res.pollLast(); }If you don't want to go back , Be similar to String That kind of , A new... Can be generated at each recursive call list Pass in , So every time the operation is called list They don't affect each other ( But there are collections copy A lot of time and space overhead )
// Depth-first search + Each recursion generates a new set , Guaranteed not to interfere with other recursive branches private void pathSum2(TreeNode root, int targetSum, LinkedList<Integer> res) { if (root == null) return; // Every time the recursion comes in, it will re new A collection , This ensures that the parameters res Does not interfere with each other in each recursion , There is no need to go back LinkedList<Integer> temp = new LinkedList<>(res); temp.add(root.val); if (root.left == null && root.right == null) { if (root.val == targetSum) { ans.add(temp); } } pathSum2(root.left, targetSum - root.val, temp); pathSum2(root.right, targetSum - root.val, temp); }summary
In the recursive process, we can quickly identify whether there will be branch pollution problem , Whether backtracking operation is required , Pay special attention to List The situation of
边栏推荐
- [leetcode] ordered linked list transformation binary search tree
- Leetcode 1814 count nice pairs in an array (recommended by map)
- 字节北京23k和拼多多上海28K,我该怎么选?
- MATLAB数组其他常见操作笔记
- 2021-2-26 compilation of programming language knowledge points
- Tencent Cloud Database tdsql - Dajia comments | The Past, Present and Future of basic software
- Px4 installation tutorial (VI) vertical fixed wing (tilting)
- Multipartfile and file interoperation tool classes
- LeetCode 1749 Maximum Absolute Sum of Any Subarray (dp)
- QGC ground station tutorial
猜你喜欢

1.3 introduction to ROS UAV

晚餐阿帮的手艺

神经网络极简史,神经网络知识点整理
![[leetcode] restore binary search tree](/img/92/14c4d670f318f93297040241a61c41.jpg)
[leetcode] restore binary search tree

threejs:如何获得几何体的boundingBox?

On permutation and Combination in Probabilistic Statistics

C语言 深度探究具有不定参数的函数

2.1 ros+px4 simulation - Fixed Point flight control

Leetcode linked list queue stack problem

Well paid test development programmers, why are you leaving? Kill each other with products until the sky is dark
随机推荐
[leetcode] delete duplicate Element II in the sorting linked list
【BSP视频教程】BSP视频教程第17期:单片机bootloader专题,启动,跳转配置和调试下载的各种用法(2022-06-10)
Leetcode 1814 count nice pairs in an array (recommended by map)
MATLAB随机函数汇总
Loki 学习总结(1)—— Loki 中小项目日志系统的不二之选
Leetcode 1094 car pooling (Analog)
2021-2-14 gephi学习笔记
2021-02-27image processing of MATLAB
flutter_ Swiper carousel map plug-in
Threejs: pit encountered in drawing Bessel curve with two-point coordinates
Kubernetes binary installation (v1.20.15) (VII) plug a work node
Leetcode 1567 maximum length of subarray with positive product
【音乐】基于matlab演奏《过火》【含Matlab源码 1875期】
Detailed explanation of classic papers on OCR character recognition
2021-02-03美赛前MATLAB的学习笔记(灰色预测、线性规划)
[leetcode] reverse linked list
Matlab array other common operation notes
Dialog alertdialog, simpledialog, showmodalbottomsheet, showtoast shutter custom dialog
The argument type ‘int?‘ can‘t be assigned to the parameter type ‘num‘
小鱼儿的处理