669. Trim a Binary Search Tree
来源:互联网 发布:淘宝店铺0信誉出售 编辑:程序博客网 时间:2024/06/06 05:33
我写的:
/**
* Definition for a binary tree node.* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode trimBST(TreeNode root, int L, int R) {
if(root==null){
return null;
}
if(root.val<L){
if(root.right==null){
return null;
}
return trimBST(root.right,L,R);
}
if(root.val>R){
if(root.left==null){
return null;
}
return trimBST(root.left,L,R);
}
if(root.val==L){
if(root.right==null){
root.left=null;
return root;
}
TreeNode node=trimBST(root.right,L,R);
root.right=node;
root.left=null;
return root;
}
if(root.val==R){
if(root.left==null){
root.right=null;
return root;
}
TreeNode node=trimBST(root.left,L,R);
root.left=node;
root.right=null;
return root;
}
TreeNode node1=null;
TreeNode node2=null;
if(root.left!=null)
node1=trimBST(root.left,L,R);
if(root.right!=null)
node2=trimBST(root.right,L,R);
root.left=node1;
root.right=node2;
return root;
}
}
不知名大佬写的:
public TreeNode trimBST(TreeNode root, int L, int R) { if(root == null)returnnull;if(root.val >= L && root.val <= R) { root.left = trimBST(root.left, L, R); root.right = trimBST(root.right, L, R);return root; } if(root.val < L) return trimBST(root.right, L, R); if(root.val > R) return trimBST(root.left, L, R);}
阅读全文
0 0
- leetcode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- LeetCode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- leetcode: 669. Trim a Binary Search Tree
- <LeetCode>669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- LeetCode 669. Trim a Binary Search Tree
- [LeetCode] 669.Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 数学知识
- C#批量修改文件名
- .NET简谈组件程序设计之(异步委托)
- Android--Notification基础介绍
- 深度学习之基础网络-SqueezeNet
- 669. Trim a Binary Search Tree
- python 匿名函数 lambda
- ORM框架理解
- 安装memcache,配置php中memcache拓展
- 接触学习linux系统服务搭建两年来的心得
- 了解HTTP (每天看十页更新)
- Mine Number
- 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方
- Numpy-ufunc函数