Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Example 1:
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
Example 2:
Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The tree does not contain a node with value = 0.
Example 3:
Input: root = [], key = 0
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 10^4].
-10^5 <= Node.val <= 10^5
Each node has a unique value.
root is a valid binary search tree.
-10^5 <= key <= 10^5
Follow up: Could you solve it with time complexity O(height of tree)?
Solution
Approach #0
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcdeleteNode(root *TreeNode, key int) *TreeNode {var pre *TreeNode cur := rootfor cur !=nil {if cur.Val == key {if cur.Left ==nil {if cur == root {return root.Right }if pre.Val > cur.Val { pre.Left = cur.Right } else { pre.Right = cur.Right }return root }if cur.Right ==nil {if cur == root {return root.Left }if pre.Val > cur.Val { pre.Left = cur.Left } else { pre.Right = cur.Left }return root } target := cur pre = cur cur = cur.Rightfor cur.Left !=nil { pre = cur cur = cur.Left }if pre.Val > cur.Val { pre.Left = cur.Right } else { pre.Right = cur.Right } target.Val = cur.Valreturn root } pre = curif cur.Val > key { cur = cur.Left } else { cur = cur.Right } }return root}
Approach #1: Recursive
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcdeleteNode(root *TreeNode, key int) *TreeNode {switch {case root ==nil:returnnilcase root.Val > key: root.Left =deleteNode(root.Left, key)case root.Val < key: root.Right =deleteNode(root.Right, key)case root.Left ==nil:return root.Rightcase root.Right ==nil:return root.Leftdefault: cur := root.Rightfor cur.Left !=nil { cur = cur.Left } cur.Right =deleteNode(root.Right, cur.Val) cur.Left = root.Leftreturn cur }return root}