Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Delete Node in a BST

Description

Flashcards on Delete Node in a BST, created by Suhas S on 16/02/2018.
Suhas S
Flashcards by Suhas S, updated more than 1 year ago
Suhas S
Created by Suhas S over 7 years ago
1
0
1 2 3 4 5 (0)

Resource summary

Question Answer
Delete Node in a BST class Solution { public TreeNode deleteNode(TreeNode root, int key) { if(root == null) return root; if(key < root.val){ root.left = deleteNode(root.left,key); }else if(key > root.val){ root.right = deleteNode(root.right, key); }else{ if(root.left == null) return root.right; else if(root.right == null) return root.left; TreeNode minNode = findMin(root.right); root.val = minNode.val; root.right = deleteNode(root.right, root.val); } return root; } private TreeNode findMin(TreeNode node){ while(node.left!=null) node = node.left; return node; } }
Show full summary Hide full summary

0 comments

There are no comments, be the first and leave one below:

Similar

Delete a node with a key
Suhas S
Remove Nth Node From End of List
Suhas S
Swap Nodes in Pairs
Suhas S
Merge Two Sorted Lists
Suhas S
Odd Even Linked List
Suhas S
Rotate List
Suhas S
Add Two Numbers
Suhas S
Copy List with Random Pointer
Suhas S
W.B. Yeats
Éimear Buggy
GCSE - Introduction to Economics
James Dodd