This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example 1: | |
| // Input: root = [4,2,7,1,3,6,9] | |
| // Output: [4,7,2,9,6,3,1] | |
| // Example 2: | |
| // Input: root = [4,2,7,1,3,6] | |
| // Output: [4,7,2,6,3,1] | |
| // Example 3: | |
| // Input: root = [2,1,3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TreeNode { | |
| constructor(value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| } | |
| class BinarySearchTree { | |
| constructor() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function insert(tree, value) { | |
| tree.push(value); | |
| return tree; | |
| } | |
| function deleteNode(tree, value) { | |
| const index = tree.indexOf(value); | |
| if (index === -1) { | |
| return tree; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Root | |
| // | | |
| // | | |
| // 11 12 | |
| // | | | |
| // | | | |
| // 13 14 15 16 | |
| // | | |
| // | | |
| // 17 18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Binary tree represented as an array | |
| // 1 | |
| // / \ | |
| // 2 3 | |
| // / \ \ | |
| // 4 5 6 | |
| const tree = [1, 2, 3, 4, 5, null, 6]; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Binary tree represented as an array | |
| // 1 | |
| // / \ | |
| // 2 3 | |
| // / \ \ | |
| // 4 5 6 | |
| const tree = [1, 2, 3, 4, 5, null, 6]; | |
| function dfs(tree) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Binary tree represented as an array | |
| // 1 | |
| // / \ | |
| // 2 3 | |
| // / \ \ | |
| // 4 5 6 | |
| const tree = [1, 2, 3, 4, 5, null, 6]; | |
| function bfs(tree) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Root | |
| // | | |
| // | | |
| // 11 12 | |
| // | | | |
| // | | | |
| // 13 14 15 16 | |
| // | | |
| // | | |
| // 17 18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Root | |
| // | | |
| // | | |
| // 11 12 | |
| // | | | |
| // | | | |
| // 13 14 15 16 | |
| // | | |
| // | | |
| // 17 18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Root | |
| // | | |
| // | | |
| // 11 12 | |
| // | | | |
| // | | | |
| // 13 14 15 16 | |
| // | | |
| // | | |
| // 17 18 |
NewerOlder