From ce4a2a15d549a8083288f5ffa283509a1ee3ca11 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Fri, 7 Mar 2025 13:35:56 +0300 Subject: [PATCH] Add solutions for BS tasks 94 and 450 --- .../450_delete_from_binary_tree/main.go | 59 ++++++++++++++ .../94_binary_tree_inorder_traversal/main.go | 35 +++++++++ neetcode/binary_trees/DFS/main.go | 78 +++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 leetcode/binary_trees/450_delete_from_binary_tree/main.go create mode 100644 leetcode/binary_trees/94_binary_tree_inorder_traversal/main.go create mode 100644 neetcode/binary_trees/DFS/main.go diff --git a/leetcode/binary_trees/450_delete_from_binary_tree/main.go b/leetcode/binary_trees/450_delete_from_binary_tree/main.go new file mode 100644 index 0000000..6b06182 --- /dev/null +++ b/leetcode/binary_trees/450_delete_from_binary_tree/main.go @@ -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 +} diff --git a/leetcode/binary_trees/94_binary_tree_inorder_traversal/main.go b/leetcode/binary_trees/94_binary_tree_inorder_traversal/main.go new file mode 100644 index 0000000..9e3145d --- /dev/null +++ b/leetcode/binary_trees/94_binary_tree_inorder_traversal/main.go @@ -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) +} diff --git a/neetcode/binary_trees/DFS/main.go b/neetcode/binary_trees/DFS/main.go new file mode 100644 index 0000000..b3ee391 --- /dev/null +++ b/neetcode/binary_trees/DFS/main.go @@ -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) +}