1
00:00:00,000 --> 00:00:04,240
A binary tree node has a value and at most
two child references.

2
00:00:04,800 --> 00:00:07,600
Each child can be the root of another
smaller tree.

3
00:00:08,080 --> 00:00:12,880
Our example visits five nodes in left
subtree, root, right subtree

4
00:00:12,960 --> 00:00:15,840
order, then calculates the greatest depth.

5
00:00:16,080 --> 00:00:20,480
We will implement that inorder traversal
with recursive calls and with an explicit

6
00:00:20,560 --> 00:00:21,040
stack.

7
00:00:21,440 --> 00:00:25,680
We will also distinguish an ordinary
binary tree from a binary search tree.

8
00:00:26,000 --> 00:00:30,560
The left subtree receives the same
traversal instruction as the whole tree.

9
00:00:30,600 --> 00:00:35,093
The current node contributes its value
after the left subtree finishes.

10
00:00:35,147 --> 00:00:38,280
The right subtree is then traversed with
the same rule.

11
00:00:38,320 --> 00:00:42,400
None represents an empty subtree and
contributes no value.

12
00:00:42,480 --> 00:00:47,280
This local contract explains recursion: if
both child calls follow the required

13
00:00:47,600 --> 00:00:51,360
order, placing the parent's value between
them gives the required order for the

14
00:00:51,480 --> 00:00:52,480
larger tree.

15
00:00:52,700 --> 00:00:56,820
Eight is the root, with three on the left
and eleven on the right.

16
00:00:57,020 --> 00:00:59,500
Three has children one and six.

17
00:00:59,540 --> 00:01:04,220
The longest path from root to leaf
contains three nodes, so the depth under

18
00:01:04,300 --> 00:01:06,540
this lesson's convention is three.

19
00:01:06,620 --> 00:01:11,420
The inorder sequence is one, three, six,
eight, eleven.

20
00:01:11,500 --> 00:01:15,260
These values happen to be increasing
because this example satisfies the

21
00:01:15,340 --> 00:01:17,100
search-tree ordering rule.

22
00:01:17,180 --> 00:01:20,860
Inorder traversal does not sort an
arbitrary binary tree.

23
00:01:21,167 --> 00:01:23,887
Return immediately when the node is None.

24
00:01:24,207 --> 00:01:28,767
Otherwise recursively visit the left child
using the same destination list.

25
00:01:28,967 --> 00:01:33,780
Append the current value after that call
finishes, then visit the right child.

26
00:01:33,834 --> 00:01:38,234
The shared result avoids repeatedly
copying complete subtree lists.

27
00:01:38,260 --> 00:01:42,807
The page's wrapper creates the result list
once and passes it to this helper.

28
00:01:42,847 --> 00:01:47,167
Every node appends once, so the
traversal's total work is linear in its

29
00:01:47,247 --> 00:01:48,287
node count.

30
00:01:48,533 --> 00:01:51,173
Push eight and follow its left child.

31
00:01:51,293 --> 00:01:53,813
Push three and follow its left child.

32
00:01:53,893 --> 00:01:56,586
Push one and reach an empty left child.

33
00:01:56,640 --> 00:02:00,813
The stack now holds eight, three, and one,
with one on top.

34
00:02:00,853 --> 00:02:04,773
These nodes have not emitted their values
yet because their left subtrees must

35
00:02:04,853 --> 00:02:05,933
finish first.

36
00:02:05,973 --> 00:02:10,453
The stack makes the recursive call history
explicit, allowing the same traversal

37
00:02:10,653 --> 00:02:12,933
without one Python call per tree level.

38
00:02:13,167 --> 00:02:16,607
Visit one by popping it and emitting its
value.

39
00:02:16,687 --> 00:02:18,487
Its right child is empty.

40
00:02:18,527 --> 00:02:22,474
Visit three next because its left subtree
is now complete.

41
00:02:22,580 --> 00:02:26,394
Push six when the traversal enters three's
right subtree.

42
00:02:26,500 --> 00:02:29,447
Visit six after reaching its empty left
child.

43
00:02:29,487 --> 00:02:33,727
The emitted values are one, three, six,
while eight remains on the

44
00:02:33,807 --> 00:02:34,607
stack.

45
00:02:34,687 --> 00:02:38,767
That pending root shows that the whole
left subtree finishes before the root

46
00:02:38,927 --> 00:02:40,047
value appears.

47
00:02:40,267 --> 00:02:43,187
Visit eight and emit the root value.

48
00:02:43,227 --> 00:02:46,147
Push eleven when entering its right
subtree.

49
00:02:46,187 --> 00:02:49,120
Visit eleven after its empty left child.

50
00:02:49,174 --> 00:02:53,147
Its right child is also empty, and the
stack is now empty, so the loop

51
00:02:53,227 --> 00:02:54,227
terminates.

52
00:02:54,267 --> 00:02:58,160
The iterative result matches the recursive
result exactly.

53
00:02:58,214 --> 00:03:02,827
A node enters the explicit stack once and
leaves once, even though the code

54
00:03:02,907 --> 00:03:06,347
contains an outer loop and an inner left-
descent loop.

55
00:03:06,567 --> 00:03:10,727
Continue while a current node or a pending
stack entry remains.

56
00:03:10,807 --> 00:03:14,647
The inner loop follows left children and
stores their ancestors.

57
00:03:14,767 --> 00:03:19,607
Pop the nearest ancestor after the descent
stops, append its value, and enter

58
00:03:19,687 --> 00:03:21,127
its right subtree.

59
00:03:21,287 --> 00:03:25,527
Testing only current would stop too early
when a left child is empty but pending

60
00:03:25,607 --> 00:03:27,100
ancestors remain.

61
00:03:27,234 --> 00:03:31,287
Testing only the stack would omit the
initial root before it is pushed.

62
00:03:31,533 --> 00:03:34,013
An empty tree has depth zero.

63
00:03:34,093 --> 00:03:37,573
For a nonempty node, compute the depth of
each child.

64
00:03:37,613 --> 00:03:41,346
Take the larger child depth and add one
for the current node.

65
00:03:41,400 --> 00:03:46,293
A leaf therefore has depth one, and the
five-node example has depth three.

66
00:03:46,333 --> 00:03:50,493
State this convention before coding
because another definition can count edges

67
00:03:50,573 --> 00:03:54,333
instead of nodes and use a different
empty-tree base case.

68
00:03:54,567 --> 00:03:59,447
Every traversal visits all n nodes, so its
time is big O of n.

69
00:03:59,487 --> 00:04:04,327
The recursive or explicit stack uses big O
of h auxiliary space, where

70
00:04:04,567 --> 00:04:06,274
h is the maximum depth.

71
00:04:06,300 --> 00:04:09,794
The returned values use another big O of n
space.

72
00:04:09,820 --> 00:04:14,407
A balanced tree has logarithmic height,
while a chain has height n.

73
00:04:14,567 --> 00:04:19,447
Python also limits recursive call depth,
so a long chain can raise RecursionError.

74
00:04:19,667 --> 00:04:24,307
Every key in the left subtree must be
smaller than the node, and every key in

75
00:04:24,347 --> 00:04:28,200
the right subtree must be larger when keys
are distinct.

76
00:04:28,334 --> 00:04:32,760
Checking only immediate children does not
enforce this whole-subtree rule.

77
00:04:32,814 --> 00:04:37,587
A search follows one child at each step,
so it costs big O of h, not

78
00:04:37,667 --> 00:04:39,867
automatically big O of log n.

79
00:04:39,907 --> 00:04:44,867
Test an empty tree, a singleton, a chain,
and an unordered tree.
