二叉树设计
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
1. 二叉搜索树中的搜索
题目描述
力扣链接
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
示例 1:
复制代码
1
2
3输入:root = [4,2,7,1,3], val = 2 输出:[2,1,3]
代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root == null) return null; TreeNode node = root; while(node != null){ if(val == node.val) return node; if(val < node.val) node = node.left; else if(val > node.val) node = node.right; } return null; } }
2. 验证二叉搜索树(双指针 + 中序遍历)
题目描述
力扣链接
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
-
节点的左子树只包含 小于 当前节点的数。
-
节点的右子树只包含 大于 当前节点的数。
-
所有左子树和右子树自身必须也是二叉搜索树。
示例 1:
复制代码
1
2
3输入:root = [2,1,3] 输出:true
代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42//(双指针 + 中序遍历) class Solution { public boolean isValidBST(TreeNode root) { if(root == null) return true; TreeNode pre = null; //定义前置指针 Deque<TreeNode> stack = new ArrayDeque<>(); TreeNode cur = root; while(cur != null || !stack.isEmpty()){ if(cur != null){ stack.push(cur); cur = cur.left; }else{ cur = stack.pop(); //vist if(pre != null && pre.val >= cur.val){ return false; } pre = cur; cur = cur.right; } } return true; } } //递归 class Solution { //记录前一个节点 long val = Long.MIN_VALUE; boolean flag = true; public boolean isValidBST(TreeNode root) { if(root == null) return flag; isValidBST(root.left); if(root.val <= val){ flag = false; } val = root.val; isValidBST(root.right); return flag; } }
3. 删除二叉搜索树中的节点
题目描述
力扣链接
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。
示例 1:
复制代码
1
2
3
4
5
6输入:root = [5,3,6,2,4,null,7], key = 3 输出:[5,4,6,2,null,null,7] 解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。 一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。 另一个正确答案是 [5,2,6,null,4,null,7]。
代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution { public TreeNode deleteNode(TreeNode root, int key) { root = delete(root,key); return root; } private TreeNode delete(TreeNode root, int key) { if (root == null) return null; if(root.val > key) root.left = delete(root.left, key); else if(root.val < key) root.right = delete(root.right, key); else{//删除逻辑 if(root.left == null) return root.right; //左子树为空,直返回右子树 if(root.right == null) return root.left; //有子树为空,直返回做子树 //选取右子树的最左叶子结点作为删除结点的根节点 TreeNode tmp = root.right; //找到最左叶子节点 while (tmp.left != null) { tmp = tmp.left; } //将该节点赋给当前结点,其实是一种交换逻辑 root.val = tmp.val; //然后将该叶子结点删除 root.right = delete(root.right,tmp.val); } return root; } }
最后
以上就是火星上茉莉最近收集整理的关于二叉搜索树二叉树设计1. 二叉搜索树中的搜索2. 验证二叉搜索树(双指针 + 中序遍历)3. 删除二叉搜索树中的节点的全部内容,更多相关二叉搜索树二叉树设计1.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复