AlgoViz
HomeBinary Trees

Binary Trees

Traversals and divide-and-conquer over left and right subtrees.

0/ 42 understood · 0%
Walkthroughs
27
Problems
42
Start here
42 shown
  1. Tree TraversalsInorder · Preorder · PostorderEasy
  2. Maximum Depth1 + max(left, right)Easy
  3. Diameter of Binary TreeLongest path through any nodeMedium
  4. Invert Binary TreeSwap children everywhereEasy
  5. Lowest Common AncestorWhere the two search paths splitMedium
  6. Practice problems
  7. Introduction to TreesA root, and up to two children eachEasy
  8. Binary Tree Representation in JavaA node class with two referencesEasy
  9. Pre, Post, Inorder in one traversalOne stack, a visit counter per nodeEasy
  10. Preorder TraversalRoot, then left, then rightEasy
  11. Inorder Traversal of Binary TreeLeft, then root, then rightEasy
  12. Postorder TraversalLeft, then right, then rootEasy
  13. Level Order TraversalA queue, one level per passEasy
  14. Iterative Preorder Traversal of Binary TreeA stack, right child pushed firstEasy
  15. Iterative Inorder Traversal of Binary TreeDescend left, pop, then go rightEasy
  16. Post-order Traversal of Binary Tree using 2 stackBuild root-right-left, then reverse itEasy
  17. Post-order Traversal of Binary Tree using 1 stackTrack the last node you emittedEasy
  18. Preorder, Inorder, and Postorder Traversal in one TraversalThe same three-state stack walkEasy
  19. Maximum Depth in BTOne plus the deeper childMedium
  20. Check for balanced binary treeReturn the height, or a failure sentinelMedium
  21. Maximum path sumA path may bend once, at its highest nodeMedium
  22. Check if two trees are identical or notSame value, same shape, recursivelyMedium
  23. Zig Zag or Spiral TraversalLevel order, reversing alternate rowsMedium
  24. Boundary TraversalLeft edge, leaves, right edge reversedMedium
  25. Vertical Order TraversalGive each node a (column, row) coordinateMedium
  26. Top View of BTFirst node seen in each columnMedium
  27. Bottom view of BTLast node seen in each columnMedium
  28. Right/Left View of Binary TreeLast node of each levelMedium
  29. Symmetric Binary TreeCompare left against right, mirroredMedium
  30. Print root to leaf path in BTAppend on the way down, remove on the way backMedium
  31. LCA in BTThe node where the two searches meetHard
  32. Maximum Width of BTIndex nodes as if the tree were an arrayMedium
  33. Children Sum Property in Binary TreePush values down, then fix on the way upMedium
  34. Print all nodes at a distance of K in BTAdd parent pointers, then BFS outwardHard
  35. Minimum time taken to burn the BT from a given NodeBFS from the target, counting levelsHard
  36. Count total nodes in a complete BTA perfect subtree can be counted by formulaEasy
  37. Requirements needed to construct a unique BTInorder plus one other orderMedium
  38. Construct a BT from Preorder and InorderPreorder gives the root, inorder splits the sidesHard
  39. Construct the Binary Tree from Postorder and Inorder TraversalPostorder gives the root, read from the endHard
  40. Serialize and De-serialize BTPreorder with explicit nullsHard
  41. Morris Preorder Traversal of a Binary TreeThread the tree instead of using a stackHard
  42. Morris Inorder Traversal of a Binary TreeThe same threads, recorded laterHard
  43. Flatten Binary Tree to Linked ListRight-child chain in preorderMedium