【算法】删除+修建二叉搜索树

450. 删除二叉搜索树中的节点

 class Solution {
     public TreeNode deleteNode(TreeNode root, int key) {
         root = delete(root, key);
         return root;
    }
     public TreeNode delete(TreeNode root, int key){
         //首先判断root是否为空,为空后续操作会报异常
         if(root == null)
             return null;
         //前序遍历,相等则处理当前节点,不相等则不处理当前节点
         if(root.val == key){
             if(root.left == null && root.right == null)
                 return null;
             else if(root.left == null && root.right != null)
                 return root.right;
             else if(root.right == null && root.left != null)
                 return root.left;
             else if(root.left != null && root.right != null){
                 TreeNode node = root.right;
                 while(node.left != null){
                     node = node.left;
                }
                 node.left = root.left;
                 return root.right;
            }
        }
         //处理左右节点
         if(key < root.val)
             root.left = delete(root.left, key);
         if(key > root.val)
             root.right = delete(root.right, key);
         //处理完后,返回当前节点
         return root;
    }
 }

669. 修剪二叉搜索树

 class Solution {
     public TreeNode trimBST(TreeNode root, int low, int high) {
         root = traversal(root, low, high);
         return root;
    }
 ​
     public TreeNode traversal(TreeNode root, int low, int high){
         if(root == null)
             return null;
         
         root.left = traversal(root.left, low, high);
         root.right = traversal(root.right, low, high);
         if(root.val > high || root.val < low){
             if(root.left == null && root.right == null)
                 return null;
             else if(root.left != null && root.right == null)
                 return root.left;
             else if(root.left == null && root.right != null)
                 return root.right;
             else if(root.left != null && root.right != null){
                 TreeNode node = root.right;
                 while(node.left != null){
                     node = node.left;
                }
                 node.left = root.left;
                 return root.right;
            }
        }
         //二叉树真是太tm神奇了,和《删除节点》一样的代码,前序遍历只处理一个节点就返回,改为后续遍历之后就可以处理全局(因为后续遍历是天然回溯,是自底向上;前序是,自顶向下)
         //这种方法,不仅仅适用于二叉搜索树,具有普适性
         return root;
    }
 }
 ​
//这种方法仅仅适用于
 class Solution {
     public TreeNode trimBST(TreeNode root, int low, int high) {
         root = traversal(root, low, high);
         return root;
    }
 ​
     public TreeNode traversal(TreeNode root, int low, int high){
         if(root == null)
             return null;
         //前序遍历
         if(root.val < low)
             return traversal(root.right, low, high);
         if(root.val > high)
             return traversal(root.left, low, high);
         //开始处理左右节点
         root.left = traversal(root.left, low, high);
         root.right = traversal(root.right, low, high);        
 ​
         return root;
    }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值