中级农民
- 积分
- 118
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-10-6
- 最后登录
- 1970-1-1
|
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 qweasdzxc2019 于 2020-5-19 14:39 编辑
https://leetcode.com/problems/find-bottom-left-tree-value/
找一棵树最下一层最左边的节点
- class Solution {
- int res = 0;
- public int findBottomLeftValue(TreeNode root) {
- helper(root, 1, 0);
- return res;
- }
- public void helper(TreeNode root, int level, Integer j){
- if(root == null) return;
- if(level > j){
- res = root.val;
- j++;
- }
- helper(root.left, level + 1, j);
- helper(root.right, level + 1, j);
- }
- }
复制代码
j是Integer引用类型的参数,为什么在运行helper(root.right, level + 1, j)时,j 的值不是 helper(root.left, level + 1, j); 访问最下一层的时候修改的j值呢?
如果把j改成全局变量就可以了,或者放在一个object里也可以,为什么作为Integer不行呢?
|
上一篇: List<List<Integer>>的问题下一篇: 讨论一下面试技巧
|