首页 > 其他 > 详细

LF.53.Delete In Binary Search Tree

时间:2018-03-27 10:16:09      阅读:213      评论:0      收藏:0      [点我收藏+]

 

Delete the target key K in the given binary search tree if the binary search tree contains K. Return the root of the binary search tree.

Find your own way to delete the node from the binary search tree, after deletion the binary search tree‘s property should be maintained.

Assumptions

There are no duplicate keys in the binary search tree

 

//==== this code is not right. needs to be updated 

 1 public class Solution {
 2   public TreeNode delete(TreeNode root, int key) {
 3     // Write your solution here.
 4     if (root == null) {
 5         return null ;
 6     }
 7     //if found, then set the root to null(means the following leafs will be removed )
 8     if (root.key == key) {
 9         return null;
10     }
11     if (root.key < key) {
12         //go to the right
13         root.right = delete(root.right, key);
14     }
15     if (root.key > key) {
16         root.left = delete(root.left, key) ;
17     }
18     return root;
19   }
20 }

 

LF.53.Delete In Binary Search Tree

原文:https://www.cnblogs.com/davidnyc/p/8655271.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!