Тёмный

Serialize and Deserialize Binary Tree - Preorder Traversal - Leetcode 297 - Python 

NeetCode
Подписаться 778 тыс.
Просмотров 99 тыс.
50% 1

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
Problem Link: neetcode.io/problems/serializ...
0:00 - Read the problem
1:32 - Drawing Explanation
8:20 - Coding Explanation
leetcode 297
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#preorder #traversal #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Наука

Опубликовано:

 

4 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 88   
@NeetCode
@NeetCode 3 года назад
🌲 Tree Playlist: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-OnSn2XEQ4MY.html
@tanayshah275
@tanayshah275 3 года назад
Awesome Explanation! posting code for deserializing without using global variables, just in case anyone wants to take a look def deserialize(self, data): nodes = data.split(',') def dfs(i): if nodes[i] == 'N': return (None, i + 1) node = TreeNode(int(nodes[i])) i += 1 node.left, i = dfs(i) node.right, i = dfs(i) return (node, i) return dfs(0)[0]
@linchuantian623
@linchuantian623 2 года назад
hi I have a question so if a tree looks like tree= [2 , 3 ,4] so the 2.left=tree[i+1] and 2.right=tree[i+2] right? I am very confused by he said that because it is recursion we only need to do addition 1 time
@ahyeoncho7685
@ahyeoncho7685 3 года назад
I really appreciate these videos. Thank you. I watch at least 10 a day.
@NeetCode
@NeetCode 3 года назад
Thanks, I'm happy theyre helpful :)
@tonyz2203
@tonyz2203 2 года назад
that's too fast
@fefefefefee32
@fefefefefee32 Год назад
@@tonyz2203 Lol, everyone has their pace of learning. I do 10 too.
@yakeensabha655
@yakeensabha655 Год назад
I've watched this vid more than 10 times today and still don't get it 😢. I think I might be not smart enough.
@user-qlksojfieopanj
@user-qlksojfieopanj Год назад
@@yakeensabha655you got this
@ayusharora9502
@ayusharora9502 2 года назад
Have always learned to serialize (aka turn into a array of some traversal) but never learned how to deserialize, learned something new! thank you very much
@ax5344
@ax5344 3 года назад
Awesome. Please keep posting these high frequency interview questions. There is another 99 in Tree, but it"s in the hard difficulty.
@DavidDLee
@DavidDLee Год назад
If you don't fall for using in-order traversal, this is quite easy. The only "hard" thing was to write a function to compare trees, if you don't get it from a platform already. Otherwise, it's hard to know if it worked / is correct. In-order traversal DFS will not work, because it will print the smallest (deepest / left-most) nodes first, and even then, it will start with null. Now, the null is ambiguous on decode, unless you do something extra, such as encoding depth or node start.
@BostonzPROPHET
@BostonzPROPHET Год назад
Thanks for the explanation! I was close with the serialization except I was using a string and adding in the commas instead of using the ",".join method on an array. I was also pretty stuck on the deserialization since I had not added the "N". This video was very helpful.
@farshadsaberi2740
@farshadsaberi2740 2 года назад
Great explanation and straightforward solution!
@matthewtang1490
@matthewtang1490 3 года назад
As soon as I get to the LC Hard problems, you already have a video posted for it :)
@sauravdeb8236
@sauravdeb8236 3 года назад
Awesome explanation. Please do Q&A sessions.
@dittocopys
@dittocopys 2 года назад
i should've went to art school
@yang5843
@yang5843 2 года назад
in Austria?
@dittocopys
@dittocopys 2 года назад
@@yang5843 not in Austria
@antonw8134
@antonw8134 2 года назад
m.ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-0n4f-VDjOBE.html
@PippyPappyPatterson
@PippyPappyPatterson 2 года назад
@@dittocopys Where is Not-In-Austria?
@prasadm3614
@prasadm3614 8 месяцев назад
Lol.... I'm with you
@DesktopDev_mfaisaltariq
@DesktopDev_mfaisaltariq 2 года назад
For deserialization you can use the Iteratable object with Next in python instead of using a global variable def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ if not data: return None nodes = iter(data.split(',')) return self.buildTree(nodes) def buildTree(self,nodes): val = next(nodes) if val == 'none': return node = TreeNode(str(val)) node.left = self.buildTree(nodes) node.right = self.buildTree(nodes) return node
@markolainovic
@markolainovic Год назад
Nice!
@amberfatima2777
@amberfatima2777 9 месяцев назад
or you can use nonlocal i in your inner function
@bryand7958
@bryand7958 3 года назад
Your channel is awesome.
@AmolGautam
@AmolGautam 10 месяцев назад
thanks for such simple solutions,
@DJ-wo9nd
@DJ-wo9nd Год назад
How does it know to switch to nodes.right in the deserialize methodto get the correct right node value? I can visualize recursion through the tree for nodes.left and nodes.right but this is setting it.
@Sookuto
@Sookuto 2 года назад
Thank you!
@edwardteach2
@edwardteach2 2 года назад
U a God
@---el6pq
@---el6pq 2 года назад
Java solution using BFS: public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { if (root == null) return ""; StringBuilder res = new StringBuilder(); Deque dfs = new LinkedList(); int size = 1; dfs.addFirst(root); TreeNode temp = root; while (size > 0) { for (int i = 0; i < size; i++) { temp = dfs.pollLast(); if (temp == null) res.append(",null"); else { res.append("," + temp.val); dfs.addFirst(temp.left); dfs.addFirst(temp.right); } } size = dfs.size(); } res.deleteCharAt(0); return res.toString(); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { if (data.equals("")) return null; List nodes = new ArrayList(Arrays.asList(data.split(","))); while (nodes.get(nodes.size() - 1).equals("null")) nodes.remove(nodes.size() - 1); int fast = 1; Deque dfs = new LinkedList(); TreeNode head = new TreeNode(Integer.valueOf(nodes.get(0))); TreeNode temp = head; dfs.addFirst(head); int size = 1; while (size > 0) { for (int i = 0; i < size; i++) { temp = dfs.pollLast(); if (temp == null) continue; if (fast >= nodes.size()) break; if (!nodes.get(fast).equals("null")) temp.left = new TreeNode(Integer.valueOf(nodes.get(fast))); fast++; if (fast >= nodes.size()) break; if (!nodes.get(fast).equals("null")) temp.right = new TreeNode(Integer.valueOf(nodes.get(fast))); fast++; dfs.addFirst(temp.left); dfs.addFirst(temp.right); } size = dfs.size(); } return head; } }
@mayukhnath1017
@mayukhnath1017 3 года назад
Great explanation. I have a question: Why don't we have to declare res=[ ] as a global variable(self.res=[ ] ) like we are declaring self.i ?
@zhaozheng7704
@zhaozheng7704 2 года назад
Int is immutable. When you try to change the value of "i" inside the nested function, a new integer object is created, and the local variable is rebound to the new object. When returning from the function, that's lost, and the value of "i" remain unchanged. In contrast, list is mutable.
@dollyvishwakarma2
@dollyvishwakarma2 2 года назад
Basically lists are mutable, so when you pass a list to a function, whatever changes the function does to the list are reflected in the original list --> in a way the list behaves as a global variable but the same is not true for a variable (integer i here in this solution). Once you perform any assignment operation on a variable inside the function, that variable is treated as a local variable, thus it is important to declare the variable as global explicitly.
@techwills4619
@techwills4619 3 года назад
Another approach : {binary tree}--------(leetcode 94 and 144) ----->{preorder, inorder} --------(leetcode 105)------> (binary tree}
@DmitriyKl
@DmitriyKl 8 месяцев назад
This is what I came up with too! Like @kevinburgerking1591 said, the solution won't work "as is" because of duplicate values. My solution was to make each value unique by appending some arbitrary character (like "@") to every value, append "L" or "R", and append the level of the node. In that case elements in the encoded string will look like "4@L15" - element with value 4 in the left subtree on the 15th level. It's a very hacky solution, but I really wanted to make it work since I realized the caveat of unique values way too late
@VasudevaK
@VasudevaK Месяц назад
@@DmitriyKl Why do you think it wouldn't work with duplicate values?
@ddshoo5282
@ddshoo5282 24 дня назад
level order / bfs: # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Codec: #Encodes a tree to a single string. def serialize(self, root: TreeNode) -> str: if not root: return "" q = deque() q.append(root) res = [] res.append(str(root.val)) while q: cur = q.popleft() if cur.left: q.append(cur.left) res.append(str(cur.left.val)) else: res.append("N") if cur.right: q.append(cur.right) res.append(str(cur.right.val)) else: res.append("N") return ",".join(res) # Decodes your encoded data to tree. def deserialize(self, data: str) -> TreeNode: if not data: return None data = data.split(",") q = deque() root = TreeNode(data[0]) q.append(root) i = 1 while q: cur = q.popleft() if data[i] != "N": left = TreeNode(data[i]) cur.left = left q.append(left) i += 1 if data[i] != "N": right = TreeNode(data[i]) cur.right = right q.append(right) i += 1 return root
@monicawang8447
@monicawang8447 2 года назад
Heyy I have a question: for the tree deserialization part, why wouldn't it run into error of exceeding boundary as we keep incrementing self.i? Which part prevents this issue? Thanks!
@yang5843
@yang5843 2 года назад
Can you provide a specific test case?
@douglaswong6560
@douglaswong6560 2 года назад
self.i is bounded by res[self.i] == "N"
@ajinkyap4246
@ajinkyap4246 Год назад
Because the last 2 values in the array will always be null / N so both will be popped out and won't call left and right on that.
@Babe_Chinwendum
@Babe_Chinwendum Год назад
I have the same confusion
@leonscander1431
@leonscander1431 11 дней назад
I thought it was impossible to solve it only with preorder traversal, because of the previous problem where we were given 2 traversals (preorder and inorder) to build tree from. That confused me so I gave up. The only idea I implemented was converting tree to a json string.
@rsKayiira
@rsKayiira Год назад
Great video just to be specific this is bottom up DFS
@samrey8134
@samrey8134 2 года назад
Thank you so much.... 😭😭😭
@dorondavid4698
@dorondavid4698 2 года назад
Quick question; does python not have a post incrementor (i++)? Or you just like doing i += 1? I've seen you do this in other vids so was just wondering
@Lambyyy
@Lambyyy 2 года назад
Correct, we just use i += 1.
@harshtiku3240
@harshtiku3240 2 года назад
python does not have a post incrementor. i+=1 or i = i+1 are the only two choices
@chaitanyayeole4111
@chaitanyayeole4111 2 месяца назад
Can we do this using just inorder traversal for serialization?
@akshayiithyd
@akshayiithyd 6 месяцев назад
I recently tried to serialize using BFS, but I am getting a Memory Limit Exceeded error the second last test case(which is almost a linked list). Has anyone done this with BFS?
@ddshoo5282
@ddshoo5282 24 дня назад
yeah, that was my first thought since the example was level order # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Codec: #Encodes a tree to a single string. def serialize(self, root: TreeNode) -> str: if not root: return "" q = deque() q.append(root) res = [] res.append(str(root.val)) while q: cur = q.popleft() if cur.left: q.append(cur.left) res.append(str(cur.left.val)) else: res.append("N") if cur.right: q.append(cur.right) res.append(str(cur.right.val)) else: res.append("N") return ",".join(res) # Decodes your encoded data to tree. def deserialize(self, data: str) -> TreeNode: if not data: return None data = data.split(",") q = deque() root = TreeNode(data[0]) q.append(root) i = 1 while q: cur = q.popleft() if data[i] != "N": left = TreeNode(data[i]) cur.left = left q.append(left) i += 1 if data[i] != "N": right = TreeNode(data[i]) cur.right = right q.append(right) i += 1 return root
@shwethaks7994
@shwethaks7994 3 года назад
Boundary of Binary Tree please can you make a video of it.
@lukelyu3264
@lukelyu3264 16 дней назад
Wondering why the tree build-up is kinda off when running the deserialize function separately: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Codec: def deserialize(self): vals = ['1','2','3','N','N', '4','5'] self.i = 0 def dfs(): if vals[self.i] == "N": self.i += 1 return None node = TreeNode(int(vals[self.i])) self.i += 1 node.left = dfs() node.right = dfs() return node return dfs() run = Codec() run.deserialize()
@abyxatjov4018
@abyxatjov4018 Год назад
Since everyone is sharing their solution, heres mine: def serialize(root): return '-' if root is None else ' '.join([root.val, serialize(root.left), serialize(root.right)]) def deserialize_rec(s): def deserialize_aux(tree): temp = tree.popleft() if temp == '-': return None n = Node(temp) n.left = deserialize_aux(tree) n.right = deserialize_aux(tree) return n return deserialize_aux(deque(s.split(' ')))
@hwang1607
@hwang1607 Год назад
what is the difference between using a variable with self. vs using a nonlocal variable
@dineshbhaskar8561
@dineshbhaskar8561 11 месяцев назад
Nonlocal is used within a nested function and is used to access a non-local or a variable that is not defined in it's own body, but is defined in the body of the enclosing function. In other words it can be understood as going one level up and fetching a variable for local use. Whereas for self.i it is in the global scope and any function(method) can access this variable at any scope in the same module.
@EduarteBDO
@EduarteBDO 8 месяцев назад
For the deserialize I reversed the list then popped the last element instead of using an index: data = data.split(',') data.reverse() def createTree(lista:list) -> TreeNode: value = lista.pop() if value == 'N':return None node = TreeNode(value) node.left = createTree(lista) node.right = createTree(lista) return node return createTree(data)
@danielderese3170
@danielderese3170 2 года назад
damn it. failed final interview because of this question.
@leonscander1431
@leonscander1431 11 дней назад
Didn't you came up with any solution or they wanted you to optimize your solution?
@1murkeybadmayn
@1murkeybadmayn 6 месяцев назад
Pardon me, I'm a beginner coder. So 1:16 the output says it should be [1,2,3,N,N,4,5] but you don't explain why at 4:59 you are having the output be [1,2,N,N,3,4,N,N,5,N,N]. The former looks like level order traversal, which nobody seems to mention, but the latter is clearly preorder traversal so won't that change the output array from what the question asks?
@harishkp8631
@harishkp8631 2 года назад
why cant we create find inorder and preorder traversal and create a binary tree from that
@kryddan
@kryddan 5 месяцев назад
Because the values of the nodes are not guaranteed to be unique, unlike the similar LC question which lets you construct a tree from pre + post-order traversal, but it has that unique constraint.
@jonaskhanwald566
@jonaskhanwald566 3 года назад
WORD BREAK - solve if possible
@vibhumrajtripathi4276
@vibhumrajtripathi4276 2 года назад
But don't we need both preorder and inorder traversal to construct a tree.??
@eddieh5036
@eddieh5036 2 года назад
I think for binary "search" tree you only need one since the order is fixed. Please let me know if my understanding is wrong.
@vibhumrajtripathi4276
@vibhumrajtripathi4276 2 года назад
@@eddieh5036 yes, it's correct because we know the inorder traversal of search tree, just the sorted array.
@robertlemiesz7143
@robertlemiesz7143 2 месяца назад
why do you use recursive functions in python.
@smtp_yurzx
@smtp_yurzx Год назад
I asked chat gpt it's solution. It wrote a breadth-first search solution first then returned a depth first search solution identical to yours
@chaitanyayeole4111
@chaitanyayeole4111 2 месяца назад
Deserialization without using Global Variable def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ serialized_values = iter(data.split(",")) def dfs(): values = next(serialized_values) if values == "N": return None node = TreeNode(int(values)) node.left = dfs() node.right = dfs() return node return dfs()
@Tristan87688
@Tristan87688 2 года назад
Do it in c++ without recursion...
@albertjtyeh53
@albertjtyeh53 3 года назад
Hmm id say this is the first video of of 15 or so that was unclear to me. The logic of deserialize just didnt click. I dont understand by in incrementing i that we know that the next in line is the left and then right everytime. Please expound if possible. Thanks though.
@yimengwang7865
@yimengwang7865 2 года назад
Because that is the pattern, in serializing, we added the left first then right, similarly, when we deserialize, we are going to add left first then right
@thndesmondsaid
@thndesmondsaid 5 месяцев назад
right and to add to what @yimengwang7865 said, you deserialize the string from left to right.
@PippyPappyPatterson
@PippyPappyPatterson 2 года назад
I wonder why you used a counter `i` instead of a `deque` like in your Iterative DFS Solution to Max Depth Binary Tree [1]. If anyone has any thoughts on the advantages or disadvantages of using a counter vs a double-ended queue I'd love to hear them. [1] ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-hTM3phVI6YQ.html
@minciNashu
@minciNashu 2 года назад
you don't need a deque. you can simulate a deque with an interator applied to the list def deserialize(self, data: str) -> TreeNode: def dfs(vals) -> TreeNode: val = next(vals) if val != '#': node = TreeNode(int(val)) node.left = dfs(vals) node.right = dfs(vals) return node return None return dfs(iter(data.split(',')))
@Akaash449
@Akaash449 2 года назад
original question had "null" for denoting Null, while you used "N" for denoting null in your code. How did it not throw error ??
@kryddan
@kryddan 5 месяцев назад
"N" is never added as a node in the actual tree representation. The TreeNode() class has a default value of left and right. = None, so if you don't assign them they will be None implicitly.
@sreejith5966
@sreejith5966 2 года назад
I think it should be a medium level question have seen a lot of medium questions harder than this
@dorondavid4698
@dorondavid4698 2 года назад
You got your wish...On Leetcode it's now Medium :D
@ChrisCox-wv7oo
@ChrisCox-wv7oo 2 года назад
@@dorondavid4698 still showing as Hard for me
@GAHbl4
@GAHbl4 2 года назад
converted to java public class Codec { List list = new ArrayList(); // Encodes a tree to a single string. public String serialize(TreeNode root) { dfsSer(root); return String.join(",",list); } void dfsSer(TreeNode root){ if(root == null) { list.add("N"); return; } list.add(String.valueOf(root.val)); dfsSer(root.left); dfsSer(root.right); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { String[] desData = data.split(","); return dfsDes(desData); } int i = 0; TreeNode dfsDes(String[] data){ if(data[i].equals("N")){ i++; return null; }else{ TreeNode node = new TreeNode(Integer.parseInt(data[i])); i++; node.left = dfsDes(data); node.right = dfsDes(data); return node; } } }
Далее
Subtree of Another Tree - Leetcode 572 - Python
14:15
Просмотров 147 тыс.
Викторина от ПАПЫ 🆘 | WICSUR #shorts
00:56
The moment we stopped understanding AI [AlexNet]
17:38
Просмотров 849 тыс.
N-Queens - Backtracking - Leetcode 51 - Python
17:51
Просмотров 157 тыс.
Find Duplicate Subtrees - Leetcode 652 - Python
14:33
Implement Trie (Prefix Tree) - Leetcode 208
18:56
Просмотров 185 тыс.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Medium Google Coding Interview With Ben Awad
51:27
Просмотров 1,2 млн
КАКОЙ SAMSUNG КУПИТЬ В 2024 ГОДУ
14:59