Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- arbore_binar = { "value" : 2, "left":
- {
- "value": 7, "left": None, "right":
- {
- "value": 6, "left":
- {
- "value": 5, "left": None, "right": None
- }, "right":
- {
- "value":11, "left": None, "right": None
- },
- },
- }, "right":
- {
- "value": 5, "left": None, "right": None
- }
- }
- def printNth(tree, n):
- ans = -1;
- cnt = [0]
- def utilPrintNth(tree):
- if tree is None:
- return
- nonlocal cnt, ans
- utilPrintNth(tree['left'])
- cnt[0] += 1
- if cnt[0] == n:
- ans = tree['value']
- utilPrintNth(tree['right'])
- utilPrintNth(tree)
- return ans
- print(printNth(arbore_binar, 3))
- # Parcurgerea in inordine al unui arbore binar
- def srd(tree):
- if (tree != None):
- return srd(tree["left"]) + [tree["value"]] + srd(tree["right"])
- else:
- return []
- print("ino: " + str(srd(arbore_binar)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement