码迷,mamicode.com
首页 > 其他好文 > 详细

450. Delete Node in a BST

时间:2019-06-30 19:20:49      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:color   roo   code   while   bsp   for   col   dmi   find   

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     TreeNode findMin(TreeNode root)
12     {
13         while(root.left!=null)
14             root=root.left;
15         return root;
16     }
17     public TreeNode deleteNode(TreeNode root, int key) {
18         if(null==root)return null;
19         if(key<root.val)
20             root.left=deleteNode(root.left,key);
21         else if(key>root.val)
22             root.right=deleteNode(root.right,key);
23         else
24         {
25         if(null==root.left)
26             return root.right;
27         else if(null==root.right)
28             return root.left;
29         
30         TreeNode min=findMin(root.right);
31         root.val=min.val;
32         root.right=deleteNode(root.right,min.val);
33         }
34         return root;
35     }
36 }

 

450. Delete Node in a BST

标签:color   roo   code   while   bsp   for   col   dmi   find   

原文地址:https://www.cnblogs.com/lychnis/p/11110569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!