2017/11/7 Leetcode 日记

时间:2022-12-17 17:20:10

2017/11/7 Leetcode 日记

669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

(修改二叉搜索树,将[L, R]外的节点删除)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL) return NULL;
if(root->val < L) return trimBST(root->right, L, R);
if(root->val > R) return trimBST(root->left, L, R); root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};

c++ 未释放内存

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R, bool top = true) {
if(root == NULL) return NULL; root->left = trimBST(root->left, L, R, false);
root->right = trimBST(root->right, L, R, false); if(root->val >= L && root->val <= R) return root;
auto result = root->val < L ? root->right : root->left;
if(!top) delete root;
return result;
}
};

c++ 释放内存

2017/11/7 Leetcode 日记的更多相关文章

  1. 2017&sol;11&sol;22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 2017&sol;11&sol;21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  3. 2017&sol;11&sol;13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  4. 2017&sol;11&sol;20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  5. 2017&sol;11&sol;9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  6. 2017&sol;11&sol;6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  7. 2017&sol;11&sol;5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  8. 2017&sol;11&sol;3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  9. jingchi&period;ai 2017&period;11&period;25-26 Onsite面试

    时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...

随机推荐

  1. Oracle中的sql操作符 和分析函数

    Oracle中的操作符算术操作符:无论是在sqlserver,或者是java中,每种语言它都有算术操作符,大同小异. Oracle中算术操作符(+)(-)(*)(/) 值得注意的是:/ 在oracle ...

  2. ajaxFileUpload上传文件没反应

    调用jquery的ajaxFileUpload异步上传文件,IE浏览器不进入success问题 原因:json转换异常,ie浏览器处理后的返回json没有<pre>标签,直接是完整的jso ...

  3. 苹果审核Metadata Rejected

    最近提交了 公司的一个 app,收到了appStore拒绝的信息及邮件 拒绝后的状态改为了:metadata rejected 邮件里面有这样一句话 意思大致就是: 你的app先被显示为 Metada ...

  4. VS 调试相关

    最近用VS2013 调试遇到的一点小问题,老年痴呆做一下记录. 1. IIS 用 w3wp.exe 调试: IIS 中的文件系统与工程的保持一致,否则断点不会命中: 发布文件系统后,重启站点对应的应用 ...

  5. ng-view和ng-include之间的区别

    ng-view通过使用路由控制,可以方便的实现页面组合,但一个html文件中,只能有一个ng-view,他是可以被ctl控制的.ng-include就是将多个页面的公共页面提取出来,如header.h ...

  6. FAB、TextInputLayout及Snackbar笔记

    FloatingActionButton 由于FloatingActionButton是重写ImageView的,所有FloatingActionButton拥有ImageView的一切属性. 控制F ...

  7. &period;2-Vue源码起步(2)

    接着第一节开始继续吧(GoGoGo) 上一节把mergeOptions函数弄完了,最后返回一个options赋给了vm.$options. 这一节继续跑代码: function initMixin(V ...

  8. Java Excel导入导出(实战)

    一.批量导入(将excel文件转成list) 1. 前台代码逻辑 1)首先在html页面加入下面的代码(可以忽略界面的样式) <label for="uploadFile" ...

  9. centos7 基础命令

    一: linux基础 (1) 查看服务器的IP信息 ip add showifconfig (2) 操作网卡命令(重启网络和启用网卡) systemctl restart networksystemc ...

  10. SpringMVC 允许跨域访问 也可以选择限制指定IP 允许访问 对象的数据传输

    java ajax