Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting null into a binary search tree

Where does null get inserted into a binary search tree?

This problem is an example input for a problem on leetcode.

Input: root = [10,5,15,3,7,null,18]

If I follow a BST insertion algorithm, what do I do with that null there? Where does it end up as a node in the tree?

like image 931
bbbbbbbbb9 Avatar asked Nov 22 '25 05:11

bbbbbbbbb9


1 Answers

When you see nulls in serializations of binary trees in LeetCode test cases like that, it doesn't mean that nodes in the tree have null as data, it's showing you where in the tree nodes are missing left and/or right children. See https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation- for LeetCode's official explanation including a tree visualizer tool.

like image 61
Tané Tachyon Avatar answered Nov 24 '25 22:11

Tané Tachyon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!