Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c7c9b9d

Browse files
committed
upload solution 94
1 parent 6aeda2b commit c7c9b9d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

94 - Binary Tree Inorder Traversal.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 94 - Binary Tree Inorder Traversal
2+
3+
Difficulty: easy
4+
Done: Yes
5+
Last edited: March 13, 2022 10:05 PM
6+
Link: https://leetcode.com/problems/binary-tree-inorder-traversal/
7+
Topic: recursion, tree
8+
9+
## Problem
10+
11+
---
12+
13+
Given the `root` of a binary tree, return *the inorder traversal of its nodes' values*.
14+
15+
```
16+
Input: root = [1,null,2,3]
17+
Output: [1,3,2]
18+
```
19+
20+
![Untitled](94%20-%20Binar%20a70d4/Untitled.png)
21+
22+
## Solution
23+
24+
---
25+
26+
in order traversal of binary tree requires to visit left child node parent node, then right child node, in that order. Will do so recursively, we know to print when node is a leaf node
27+
28+
## Whiteboard
29+
30+
---
31+
32+
![Untitled](94%20-%20Binar%20a70d4/Untitled%201.png)
33+
34+
## Code
35+
36+
---
37+
38+
```python
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, val=0, left=None, right=None):
42+
# self.val = val
43+
# self.left = left
44+
# self.right = right
45+
class Solution:
46+
47+
def __init__(self):
48+
self.res = []
49+
50+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
51+
# in order traversal of binary tree requires to visit left child node,
52+
# parent node, then right child node. In that order
53+
54+
# will do so recursively
55+
# we know to print when node is a leaf node
56+
57+
if root is None:
58+
return []
59+
60+
else:
61+
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
62+
```
63+
64+
## Time Complexity
65+
66+
---
67+
68+
The time complexity is O(n)*O*(*n*) because the recursive function is $T(n)=2⋅T(n/2)+1T(n)=2⋅T(n/2)+1$.

images/94-1.png

17.8 KB
Loading

images/94-2.png

1.14 MB
Loading

0 commit comments

Comments
 (0)