let say, we want to find the bottom left node in a tree.
one way to do it is using global vars:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{ public int findBottomLeftValue(TreeNode root)
{
find(root, 0);
return d_bottomLeft.val;
} private int d_bottomDepth = -1;
private TreeNode d_bottomLeft = null;
private void find(TreeNode node, int depth)
{
if (node == null) return;
if (depth >= d_bottomDepth)
{
d_bottomDepth = depth;
d_bottomLeft = node;
} find(node.right, depth + 1);
find(node.left , depth + 1);
}
}

to avoid using global vars, we can write it in this way:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public int findBottomLeftValue(TreeNode root)
{
Result result = new Result();
find(root, 0, result);
return result.d_bottomLeft.val;
} private void find(TreeNode node, int depth, Result result)
{
if (node == null) return;
if (depth >= result.d_bottomDepth)
{
result.d_bottomDepth = depth;
result.d_bottomLeft = node;
}
find(node.right, depth + 1, result);
find(node.left , depth + 1, result);
} private class Result
{
public int d_bottomDepth = -1;
public TreeNode d_bottomLeft = null;
}
}

how to make a function from using global var to not using it的更多相关文章

  1. 【兼容调试】cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS

    重装cryptography就好了. conda uninstall cryptography conda install cryptography https://github.com/pyca/c ...

  2. Fatal error: Call to undefined function Think\C() in /var/www/html/ceshi.hzheee.com/think/ThinkPHP/Library/Think/Think.class.php on line 334 这个问题解决

    当APP_DEBUG为true时,包含图中这个文件,文件中又引导包含这些库文件,可以看出安装thinkphp3.2.3时ThinkPHP/Common/下是functions.php,把它改成func ...

  3. PHP内核探索之变量(2)-理解引用

    本文主要内容: 引论 符号表与zval 引用原理 回到最初的问题 一.引论 很久之前写了一篇关于引用的文章,当时写的寥寥草草,很多原理都没有说清楚.最近在翻阅Derick Rethans(home: ...

  4. 面向对象的JS随笔

    Scoping 全局与局部 全局变量可用在所有环境中,局部变量只可用在局部 js中连接变量至一个从未声明的变量,后面的变量自动提升成一个全局变量(不要这样用,不易阅读) 只有function(){中才 ...

  5. Javascript - 预编译与函数词法作用域

    预编译与函数词法作用域(Precompiled & Scoped) 预编译 Javascript脚本的宿主在执行代码之前对脚本做了预编译处理,比如浏览器对Js进行了预编译,编译器会扫描所有的声 ...

  6. canvas 笔记整理

    canvas Retina 屏幕优化 /** * HiDPI Canvas Polyfill (1.0.9) * * Author: Jonathan D. Johnson (http://jonda ...

  7. Global variable in ABAP function group

    Function group is loaded into runtime memory by the FIRST call of a function module inside this func ...

  8. 全局变量:global与$GLOBALS的区别和使用

    今天在写框架的时候想把SaeMySQL初始化之后作为全局变量使用.但是后来发现PHP中的全局变量和Java或者OC中的全局变量还是有较大区别的.下面记录一下php里面的global的使用相关注意事项. ...

  9. PHP中global与$GLOBALS['']的区别

    +++ 探讨(一)+++++++++++++++++++++++++++++++++++++++ 很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GL ...

随机推荐

  1. BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治

    BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治 Description 你是任意性复杂机器公司(Arbitrarily Complex Machines, ACM) ...

  2. page-break-before和page-break-after

    page-break-before和page-break-after CSS属性并不会修改网页在屏幕上的显示,这两个属性是用来控制文件的打印方式. 每个打印属性都可以设定4种设定值:auto.alwa ...

  3. 任务36:应用Jwtbearer Authentication

    任务36:应用Jwtbearer Authentication D:\MyDemos\jesse 新建项目:dotnet new webapi --name JwtAuthSample VS2017运 ...

  4. 基于ANDROID平台,U3D对蓝牙手柄键值的获取

    对于ANDROID平台,物理蓝牙手柄已被封装,上层应用不可见,也就是说对于上层应用,不区分蓝牙手柄还是其它手柄: 完成蓝牙手柄和ANDROID手机的蓝牙连接后,即可以UNITY3D中获取其键值: 在U ...

  5. E20180404-ts

    aero  adj. 航空的,飞行的,飞机的; wheel   n. 轮子; 旋转; Wheels 汽车; 有…轮子的汽车(或自行车等); vi. 转动,旋转; 突然转变方向; foil n. 箔,金 ...

  6. E20170425-gg

    margin  n(网页设置,CSS) 外铺,(金融,外汇交易)保证金 property n(计算机)类的属性变量 , 资产

  7. bzoj 3676: [Apio2014]回文串【回文自动机】

    回文自动机板子 或者是SAM+manacher+倍增,就是manacher求本质不同回文串(让f++的串),然后在SAM倍增查询对应点出现次数 #include<iostream> #in ...

  8. 解决wubi安装ubuntu时要下载系统映像文件问题

    转载:一个人的旅行的博客(http://www.cnblogs.com/rollenholt/articles/2607433.html) 下面我介绍解决wubi安装ubuntu时要去官网下载系统映像 ...

  9. html 文本溢出显示省略号 .....

  10. SQL 实战语句(9)

    1.如何同步根据部门表同步另外一个项目表的部门字段 update A set A.auditownerid=(select lp.epleaderId from (select ep.userid a ...