Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions ConstructBTFromInorderPostorder.swift
Original file line number Diff line number Diff line change
@@ -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<Int, Int>()
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<Int, Int>, 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
}
}
80 changes: 80 additions & 0 deletions RootToLeafSum.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}