File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 143
143
| 645| [ 错误的集合] ( https://leetcode.cn/problems/set-mismatch/ ) | [ JavaScript] ( ./algorithms/set-mismatch.js ) | Easy|
144
144
| 654| [ 最大二叉树] ( https://leetcode.cn/problems/maximum-binary-tree/ ) | [ JavaScript] ( ./algorithms/maximum-binary-tree.js ) | Medium|
145
145
| 657| [ 机器人能否返回原点] ( https://leetcode.cn/problems/robot-return-to-origin/ ) | [ JavaScript] ( ./algorithms/robot-return-to-origin.js ) | Easy|
146
+ | 669| [ 修剪二叉搜索树] ( https://leetcode.cn/problems/trim-a-binary-search-tree/ ) | [ JavaScript] ( ./algorithms/trim-a-binary-search-tree.js ) | Medium|
146
147
| 682| [ 棒球比赛] ( https://leetcode.cn/problems/baseball-game/ ) | [ JavaScript] ( ./algorithms/baseball-game.js ) | Easy|
147
148
| 687| [ 最长同值路径] ( https://leetcode.cn/problems/longest-univalue-path/ ) | [ JavaScript] ( ./algorithms/longest-univalue-path.js ) | Medium|
148
149
| 700| [ 二叉搜索树中的搜索] ( https://leetcode.cn/problems/search-in-a-binary-search-tree/ ) | [ JavaScript] ( ./algorithms/search-in-a-binary-search-tree.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @param {number } low
12
+ * @param {number } high
13
+ * @return {TreeNode }
14
+ */
15
+ var trimBST = function ( root , low , high ) {
16
+ // 后序遍历
17
+ if ( ! root ) return null ;
18
+ root . left = trimBST ( root . left , low , high ) ;
19
+ root . right = trimBST ( root . right , low , high ) ;
20
+ if ( root . val < low || root . val > high ) {
21
+ // 叶节点
22
+ if ( ! root . left && ! root . right ) {
23
+ return null ;
24
+ }
25
+ // 左节点不存在
26
+ if ( ! root . left ) {
27
+ return root . right ;
28
+ }
29
+ // 右节点不存在
30
+ if ( ! root . right ) {
31
+ return root . left ;
32
+ }
33
+ }
34
+ return root ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments