diff --git a/ConstructBTFromInorderPostorder.swift b/ConstructBTFromInorderPostorder.swift new file mode 100644 index 00000000..d3ffc50a --- /dev/null +++ b/ConstructBTFromInorderPostorder.swift @@ -0,0 +1,52 @@ +// +// ConstructBinaryTreeFromInorderTraversal.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 1/19/26. +// + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ +class BinaryTreeFromInorderAndPostorder { + var postOrderIdx = 0 + func buildTree(_ inorder: [Int], _ postorder: [Int]) -> TreeNode? { + postOrderIdx = postorder.count - 1 + var inMap = Dictionary() + for (index, element) in inorder.enumerated() { + inMap[element] = index + } + return constructTree(postorder, inMap: inMap, inStart: 0, inEnd: inorder.count - 1) + } + + func constructTree(_ postorder: [Int], inMap: Dictionary, inStart: Int, inEnd: Int) -> TreeNode? { + //base + if (inStart > inEnd) { + return nil + } + + //logic + let rootVal = postorder[postOrderIdx] + postOrderIdx -= 1 + let root = TreeNode(val: rootVal, left: nil, right: nil) + let rootIdx = inMap[rootVal]! + + //recurse + root.right = constructTree(postorder, inMap: inMap, inStart: rootIdx + 1, inEnd: inEnd) + root.left = constructTree(postorder, inMap: inMap, inStart: inStart, inEnd: rootIdx - 1) + + return root + } +} diff --git a/RootToLeafSum.swift b/RootToLeafSum.swift new file mode 100644 index 00000000..7467cd1c --- /dev/null +++ b/RootToLeafSum.swift @@ -0,0 +1,80 @@ +// +// RootToLeafSum.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 1/15/26. +// + +/* + Whatever has gone into the stack will be out with the same values. + */ +class BinarySearchTree3 { + + var result: Int = 0 + + init() { + let node1 = TreeNode(val: 3, left: nil, right: nil) + let node2 = TreeNode(val: 9, left: nil, right: nil) + +// let pathSum = sumNumbers(root: TreeNode(val: 5, left: node1, right: node2)) +// print("paths sum \(pathSum)") + } + + func sumNumbers(_ root: TreeNode?) -> Int { + let r = helper(root, currentNum: 0) + print("r \(r)") + return r + } + + func helper(_ root: TreeNode?, currentNum: Int) -> Int { + guard let root else { + return 0 + } + + let curr = currentNum * 10 + root.val + if (root.left == nil && root.right == nil) { + return curr + } + let pathSumFromLeftSubtree = helper(root.left, currentNum: curr) + let pathSumFromRightSubtree = helper(root.right, currentNum: curr) + return pathSumFromLeftSubtree + pathSumFromRightSubtree + } + + + func rootToLeafPathSum(root: TreeNode?, currentNum: Int) -> Int { + + guard let root = root else { + return 0 + } + + let currentNum = currentNum * 10 + root.val + if (root.left == nil && root.right == nil) { + return currentNum + } + + let leftSum = rootToLeafPathSum(root: root.left, currentNum: currentNum) + let rightSum = rootToLeafPathSum(root: root.right, currentNum: currentNum) + return leftSum + rightSum + + } + + //with global result parameter. + func helper(root: TreeNode?, currentNum: Int) { + + var currentNum = currentNum + //base + guard let root else { + return + } + //logic + currentNum = currentNum * 10 + root.val + //check if the current node is a leaf + //action + if (root.left == nil && root.right == nil) { + result += currentNum + } + + helper(root: root.left, currentNum: currentNum) + helper(root: root.right, currentNum: currentNum) + } +}