[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree

Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

Example

Given binary search tree:

5

/    \

3          6

/    \

2       4

Remove 3, you can either return:

5

/    \

2          6

\

4

or :

5

/    \

4          6

/

2

Tags Expand

LintCode Copyright Binary Search Tree

哎,先来个偷懒的写法吧。分类讨论情况太多了。  T。T

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param root: The root of the binary search tree.
17      * @param value: Remove the node with given value.
18      * @return: The root of the binary search tree after removal.
19      */
20     TreeNode* buildTree(vector<TreeNode*> &v, int left, int right) {
21         if (left > right) return NULL;
22         int mid = left + ((right - left) >> 1);
23         v[mid]->left = buildTree(v, left, mid - 1);
24         v[mid]->right = buildTree(v, mid + 1, right);
25         return v[mid];
26     }
27     TreeNode* removeNode(TreeNode* root, int value) {
28         // write your code here
29         vector<TreeNode*> v;
30         TreeNode *cur = root, *tmp;
31         stack<TreeNode*> stk;
32         while (cur != NULL || !stk.empty()) {
33             if (cur != NULL) {
34                 stk.push(cur);
35                 cur = cur->left;
36             } else {
37                 cur = stk.top();
38                 stk.pop();
39                 tmp = cur;
40                 cur = cur->right;
41                 if (tmp->val != value) {
42                     v.push_back(tmp);
43                 } else {
44                     delete tmp;
45                     tmp = NULL;
46                 }
47             }
48         }
49         return buildTree(v, 0, (int)v.size() - 1);
50     }
51 };
时间: 2024-12-23 04:06:08

[LintCode] Remove Node in Binary Search Tree的相关文章

【Lintcode】087.Remove Node in Binary Search Tree

题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem

Remove Node in Binary Search Tree

Question:  Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree a

Remove Node in Binary Search Tree 解答

从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树(因为第一个比删除节点大的节点可能出现在右子树,不会出现在左子树). 这里用Target表示删除节点,Parent表示删除节点的母节点. 1. 没有右子树 Parent.left / Parent.right = Target.left 2. 有右子树 1) 找到第一个比删除节点大的节点Node 2

LintCode : Inorder Successor in Binary Search Tree

Very interesting problem! http://www.lintcode.com/zh-cn/problem/inorder-successor-in-binary-search-tree/ Description: Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST. Example: Given tr

[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

[Lintcode] Validate Binary Search Tree

Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node c