Delete node in BST
来源:互联网 发布:python url encode 编辑:程序博客网 时间:2024/06/05 17:38
Question: delete the given value in BST.
This question is pretty tricky, to delete a node in BST, there are several cases to consider.
1: The node is one of the leaves, 2: the node only has right children, 3: the node only has left children, 4: the node has left and right children.
code:
TreeNode* deleteTreeNode(TreeNode* root, int val) { if(!root) return NULL; else { if(val < root->val) root->left = deleteTreeNode(root->left, val); else if(val > root->val) root->right = deleteTreeNode(root->right, val); else { // if there is no left child, no right child if(root->left == NULL && root->right == NULL) { delete root; root = NULL; return root; } else if(root->left == NULL) { // if there is no left subtree TreeNode* tmp = root; root = root->right; delete tmp; return root; } else if(root->right == NULL) { // if there is no right subtree TreeNode* tmp = root; root = root->right; delete tmp; return root; } else { // two children struct TreeNode* tmp = findMin(root->right); root->data = tmp->data; root->right = deleteTreeNode(root->right, tmp->data); return root; } } }}
0 0
- Delete node in BST
- Delete Node in a BST
- Delete Node in a BST
- Delete Node in a BST
- Add, Search, Delete Node in BST.
- [LeetCode]Delete Node in a BST
- [hard]450. Delete Node in a BST
- 450. Delete Node in a BST
- LeetCode 450. Delete Node in a BST
- 450. Delete Node in a BST
- LeetCode 450. Delete Node in a BST
- Leetcode-450. Delete Node in a BST
- 450. Delete Node in a BST
- 【LeetCode】 450. Delete Node in a BST
- [LeetCode]450. Delete Node in a BST
- 450 Delete Node in a BST
- 450. Delete Node in a BST
- 450. Delete Node in a BST**
- springMVC 学习
- L2-005. 集合相似度-PAT团体程序设计天梯赛GPLT
- Tomcat8.x 路由表的建立->路由的匹配->Servlet的执行->数据的输出
- Android学习之service的绑定
- DeepID2+ face recognition
- Delete node in BST
- MFC窗口样式及其值(WS_Style)即CreateWindow的参数dwStyle
- 深入解析:分布式系统的事务处理经典问题及模型
- 浮点错误的意思-PAT 、OJ
- L1-009. N个数求和-PAT团体程序设计天梯赛GPLT
- HDU 1108 最小公倍数(Gcd 数论)
- Windows(WinAPI)窗口创建后,依次接收的消息
- 树莓派 Learning 002 装机后必要的操作 --- 10 实现本地电脑与远程桌面之间复制粘贴(传输)文件
- windows常用消息大全(系统消息、通告消息、用户消息)