Add solutions for BS tasks 94 and 450
This commit is contained in:
parent
9d2268e871
commit
ce4a2a15d5
3 changed files with 172 additions and 0 deletions
59
leetcode/binary_trees/450_delete_from_binary_tree/main.go
Normal file
59
leetcode/binary_trees/450_delete_from_binary_tree/main.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
// 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.
|
||||
|
||||
// Constraints:
|
||||
//
|
||||
// The number of nodes in the tree is in the range [0, 104].
|
||||
// -105 <= Node.val <= 105
|
||||
// Each node has a unique value.
|
||||
// root is a valid binary search tree.
|
||||
// -105 <= key <= 105
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func findMin(subTreeRoot *TreeNode) *TreeNode {
|
||||
for (subTreeRoot != nil) && (subTreeRoot.Left != nil) {
|
||||
subTreeRoot = subTreeRoot.Left
|
||||
}
|
||||
return subTreeRoot
|
||||
}
|
||||
|
||||
func deleteNode(root *TreeNode, key int) *TreeNode {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if key < root.Val {
|
||||
root.Left = deleteNode(root.Left, key)
|
||||
} else if key > root.Val {
|
||||
root.Right = deleteNode(root.Right, key)
|
||||
} else {
|
||||
if (root.Right == nil) && (root.Left == nil) {
|
||||
root = nil
|
||||
return root
|
||||
} else if (root.Right != nil) && (root.Left == nil) {
|
||||
root = root.Right
|
||||
return root
|
||||
} else if (root.Left != nil) && (root.Right == nil) {
|
||||
root = root.Left
|
||||
return root
|
||||
} else if (root.Left != nil) && (root.Right != nil) {
|
||||
minSubTreeNode := findMin(root.Right)
|
||||
deleteNode(root.Right, minSubTreeNode.Val)
|
||||
root.Val = minSubTreeNode.Val
|
||||
return root
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func traverse(root *TreeNode, res *[]int) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
traverse(root.Left, res)
|
||||
*res = append(*res, root.Val)
|
||||
traverse(root.Right, res)
|
||||
}
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
var result []int
|
||||
traverse(root, &result)
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
n10 := TreeNode{Val: 2}
|
||||
n9 := TreeNode{Val: 5}
|
||||
n8 := TreeNode{Val: 7}
|
||||
n7 := TreeNode{Val: 6, Left: &n9, Right: &n8}
|
||||
n6 := TreeNode{Val: 3, Left: &n10}
|
||||
n5 := TreeNode{Val: 4, Left: &n6, Right: &n7}
|
||||
|
||||
res := inorderTraversal(&n5)
|
||||
fmt.Print(res)
|
||||
}
|
78
neetcode/binary_trees/DFS/main.go
Normal file
78
neetcode/binary_trees/DFS/main.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func inorder(root *TreeNode) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
inorder(root.Left)
|
||||
fmt.Print(root.Val)
|
||||
inorder(root.Right)
|
||||
}
|
||||
|
||||
func preorder(root *TreeNode) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
fmt.Print(root.Val)
|
||||
preorder(root.Left)
|
||||
preorder(root.Right)
|
||||
}
|
||||
|
||||
func postorder(root *TreeNode) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
postorder(root.Left)
|
||||
postorder(root.Right)
|
||||
fmt.Print(root.Val)
|
||||
}
|
||||
|
||||
func reverserOrder(root *TreeNode) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
reverserOrder(root.Right)
|
||||
fmt.Print(root.Val)
|
||||
reverserOrder(root.Left)
|
||||
}
|
||||
|
||||
func traverse(root *TreeNode, res *[]int){
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
traverse(root.Left, res)
|
||||
*res = append(*res, root.Val)
|
||||
traverse(root.Right, res)
|
||||
}
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
var result []int
|
||||
traverse(root, &result)
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
n10 := TreeNode{Val: 2}
|
||||
n9 := TreeNode{Val: 5}
|
||||
n8 := TreeNode{Val: 7}
|
||||
n7 := TreeNode{Val: 6, Left: &n9, Right: &n8}
|
||||
n6 := TreeNode{Val: 3, Left: &n10}
|
||||
n5 := TreeNode{Val: 4, Left: &n6, Right: &n7}
|
||||
|
||||
// inorder(&n5)
|
||||
// fmt.Println()
|
||||
// preorder(&n5)
|
||||
// fmt.Println()
|
||||
// postorder(&n5)
|
||||
// fmt.Println()
|
||||
// reverserOrder(&n5)
|
||||
res := inorderTraversal(&n5)
|
||||
fmt.Print(res)
|
||||
}
|
Loading…
Reference in a new issue