Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

57-tgyuuAn #197

Merged
merged 2 commits into from
May 31, 2024
Merged

57-tgyuuAn #197

merged 2 commits into from
May 31, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented May 25, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํŠธ๋ฆฌ์˜ ์ˆœํšŒ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

์ธ์˜ค๋”, ํ”„๋ฆฌ์˜ค๋”, ํฌ์ŠคํŠธ์˜ค๋” (Inorder, Preorder, Postorder)

์ด ๋ฌธ์ œ ์ชผ๊ผผ์€ ์–ด๋ ต๋‹ค.

๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์—, ์œ„์— ๋งํฌ์—์„œ ๋„๋Œ€์ฒด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊นŒ ๊ณฐ๊ณฐํžˆ 20๋ถ„๋™์•ˆ ์ณ๋‹ค๋ณด๊ธฐ๋งŒ ํ–ˆ๋‹ค.







๊ฐ‘์ž๊ธฐ ๋ฌด์–ธ๊ฐ€ ๋ฒˆ๋œฉ ๋– ์˜ฌ๋ž๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์€ ํŠธ๋ฆฌ์— ํ›„์œ„ ์ˆœํšŒ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž. (Left -> Right -> Root)

image

(1) -> (4) -> (6) -> (5) -> (3) -> (12) -> (16) -> (15) -> (7)

๊ทธ๋Ÿฌ๋ฉด ๋งˆ์ง€๋ง‰์œผ๋กœ ํƒ์ƒ‰๋˜๋Š” ํ•ญ์€ ํ•ญ์ƒ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๊ฐ€ ๋œ๋‹ค.







๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฃจํŠธ ๊ฐ’ (7) ์ด ์ „์œ„ ์ˆœํšŒ ์—์„œ ์–ด๋””์— ์œ„์น˜ํ•˜๋Š” ์ง€ ์ฐพ์•„๋ณด๋‹ˆ,

image

(1) -> (3) -> (4) -> (5) -> (6) -> (7) -> (12) -> (15) -> (16)

์ •ํ™•ํžˆ ์™ผ์ชฝ ํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ๋ฅผ ๊ฐ€๋ฅด๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

image







ํ˜ธ์˜ฌ๋ฆฌ... ์ด ๊ทœ์น™์„ ์ฐพ์ž๋งˆ์ž ๋ฌด์–ธ๊ฐ€ ๋จธ๋ฆฌ ์†์—์„œ ์Šค์ณ ์ง€๋‚˜๊ฐ”๋‹ค.

๋ฌด์—‡์ธ์ง€ ์ฐพ์œผ์…จ๋‚˜์š” ...?







๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ, ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ ํŠธ๋ฆฌ๋ฅผ ๋‚˜๋ˆˆ ๋’ค

์ „์œ„ ์ˆœํšŒ, ํ›„์œ„ ์ˆœํšŒ๋ฅผ ๋ถ„ํ• ํ•˜๋ฉด ๋˜ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ...!

image







์ฆ‰ ์ด ๋ฌธ์ œ๋Š” ๋ถ„ํ•  ์ •๋ณต ์ด์—ˆ๋”ฐ!!!!!!!!







์ด ๊ฒƒ์„ ํŒŒ์•…ํ•œ ๋’ค, ์ฒ˜์Œ์—๋Š” ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ, ์™ผ์ชฝ ํŠธ๋ฆฌ๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์„ ๊ทธ๋ƒฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž˜๋ผ์„œ ๋ณต์‚ฌ ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค.

import sys

def input(): return sys.stdin.readline().rstrip()

N = int(input())
in_order = list(input().split())
post_order = list(input().split())

def divide_and_conquer(in_order, post_order):
    pre_order_root = post_order.pop()
    root_idx = in_order.index(pre_order_root)

    left_in_order_tree = in_order[:root_idx]
    right_in_order_tree = in_order[root_idx+1:] if root_idx != len(in_order) else []

    pre_order = [pre_order_root]
    temp = []
    if len(right_in_order_tree) >= 1:
        temp = divide_and_conquer(right_in_order_tree, post_order)
    
    if len(left_in_order_tree) >= 1:
        pre_order.extend(divide_and_conquer(left_in_order_tree, post_order))
    
    pre_order.extend(temp)
    return pre_order

print(" ".join(divide_and_conquer(in_order, post_order)))







ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋ฒ„๋ ธ๊ณ ,

image







๊ฒฐ๊ตญ ๋ฆฌ์ŠคํŠธ๋ฅผ ์•„์˜ˆ ๊ฑด๋“ค์ง€ ์•Š๊ณ , ์ธ๋ฑ์Šค๋กœ๋งŒ ํŠธ๋ฆฌ๋ฅผ ์ž๋ฅด๋„๋ก ๋กœ์ง์„ ๋ฐ”๊ฟจ๋‹ค.

์ด ๊ณผ์ •์—์„œ ์‹œ๊ฐ„์„ ๋„ˆ๋ฌด ๋งŽ์ด ์จ๋ฒ„๋ ธ๋‹ค.

image

์จ‹๋“  ๊ณ„์† ์Š›ํ•ด๋ณด๋ฉด์„œ ๋””๋ฒ„๊น…ํ•˜๊ณ  ์–ด๋–ป๊ฒŒ๋“  ํ’€์—ˆ๋‹ค...............

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels May 25, 2024
@tgyuuAn tgyuuAn self-assigned this May 25, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review May 26, 2024 05:18
Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „์œ„, ํ›„์œ„, ์ค‘์œ„ ์ˆœํšŒ๋„ ์˜ค๋žœ๋งŒ์— ๋ด์„œ ๊ฐœ๋… ์žก๋Š๋ผ ์—ฌ๋ ค์› ๋„ค์š”..
๋ฌธ์ œ ํ‘ธ๋Š” ์•„์ด๋””์–ด๊ฐ€ ์ƒ๊ฐ์ด ์•ˆ๋‚˜์„œ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ์น˜๋ฉด์„œ ์ดํ•ดํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค..!

lateinit var br: BufferedReader
lateinit var inOrder: List<Int>
lateinit var postOrder: List<Int>
lateinit var positions: Array<Int>

fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    inOrder = br.readLine().split(" ").map { it.toInt() }
    postOrder = br.readLine().split(" ").map { it.toInt() }
    positions = Array(n + 1) { 0 }

    // ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ช‡๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š”์ง€ ์ €์žฅ
    inOrder.forEachIndexed { index, value ->
        positions[value] = index
    }
    divideAndConquer(0, n - 1, 0, n - 1)
}

private fun divideAndConquer(inStart: Int, inEnd: Int, postStart: Int, postEnd: Int) {
    if (inStart > inEnd || postStart > postEnd) return

    // post ์˜ ๋งˆ์ง€๋ง‰์€ parent ์ด๋‹ค.
    val inOrderParent = postOrder[postEnd]
    // pre-order ๋Š” P-L-R ์ˆœ
    print("$inOrderParent ")

    // ์ธ์˜ค๋”์—์„œ Parent ๊ฐ€ ์–ด๋””์žˆ๋Š”์ง€ ์ฐพ๊ธฐ
    val parentIndex = positions[inOrderParent]

    // ์™ผ์ชฝ
    divideAndConquer(inStart, parentIndex - 1, postStart, postStart + (parentIndex - inStart) - 1)

    // ์˜ค๋ฅธ์ชฝ
    divideAndConquer(parentIndex + 1, inEnd, postStart + (parentIndex - inStart), postEnd - 1)
}

@alstjr7437
Copy link
Member

ํ˜ธ์˜ฌ๋ฆฌ.... ์–ด๋–ป๊ฒŒ ํ›„์œ„ ์ˆœํšŒ๋Š” ๋ฃจํŠธ๊ฐ€ ์ œ์ผ ๋’ค๋กœ ๊ฐ€๊ณ  ๊ทธ ๋ถ€๋ถ„์„ ์ด์šฉํ•ด์„œ ๋ฃจํŠธ ์ฐพ๊ณ 
์ „์œ„๋ฅผ ์ด์šฉํ•ด์„œ ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ๋‚˜๋ˆ„๊ณ  ๋ถ„ํ• ์ •๋ณต์„ ํ•˜๋Š” ๋ฐฉ์‹์„ ์ฐพ์€ ๋‹น์‹ ... ์‹ ์ž…๋‹ˆ๊นŒ??..๐Ÿง ๐Ÿง 

Comment on lines +17 to +18
in_order_root = post_order[post_end]
print(in_order_root, end=" ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ด์•ผํ•˜๋‚˜ ํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๊ทธ๋ƒฅ ๋ฐ”๋กœ print๋ฅผ ํ•ด์„œ ์ค‘์œ„ ์ˆœํšŒ ์ถœ๋ ฅ๋„ ๊ฐ€๋Šฅํ•˜๊ตฐ์š” ใ„ทใ„ทใ„ทใ„ท

import sys

sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜น์‹œ ๊ทธ๋ƒฅ ๊ถ๊ธˆํ•œ์ ์ธ๋ฐ

input = sys.stdin.readline

์œ„ ์ฝ”๋“œ์™€ ํƒœ๊ทœ๋‹˜์˜ ํ•จ์ˆ˜ํ™”์™€ ๋‹ค๋ฅธ์ ์ด ๋”ฐ๋กœ ์žˆ์„๊นŒ์š”??

Copy link
Member Author

@tgyuuAn tgyuuAn May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—…์‘ค๋‹ค!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜›๋‚  ๊ณผ์ œ์— postorder๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, preorder๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ์–ด์„œ
๊ทธ ์ฝ”๋“œ๋ฅผ ์ข€ ์‘์šฉํ•ด์„œ ์ œ์ถœํ–ˆ๋”๋‹ˆ ๊ณ„์† ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Šด๋‹ค..

๊ทธ๋ž˜์„œ ํƒœ๊ทœ๋‹˜ PR์ด๋ž‘ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ฐธ์กฐํ–ˆ์–ด์š”

hmmmmm.. ํŠธ๋ฆฌ ๋ฌธ์ œ๋Š” ์•ˆ์ต์ˆ™ํ•ด์„œ ๊ทธ๋Ÿฐ๊ฐ€ ์–ด๋ ต๋„คํšจ ๐ŸŒณ ๐ŸŒณ๐ŸŒณ๐ŸŒณ๐ŸŒณ๐ŸŒณ๐ŸŒณ

์ˆ˜๊ณ ํ•˜์…จ์Šด๋‹ค~!

def findPreorder(start, end, postStart, postEnd):
    if postStart > postEnd:
        return

    root = postorder[postEnd]
    print(root, end=" ")
    rIdx = inorderIndex[root]

    leftLength = rIdx - start

    findPreorder(start, rIdx - 1, postStart, postStart + leftLength - 1)
    findPreorder(rIdx + 1, end, postStart + leftLength, postEnd - 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants