Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions MaximumDepthOfBinaryTree/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
```java
// Use DFS, return depth when node don't have children
// Increse depth before visit child node
// Time complexity: O(N) N: number of node
// Space complexity: O(N)
// Time spend: 04:26
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

// BFS
// Time spend: 04:30
public int maxDepthFindByBfs(TreeNode root) {
if (root == null) {
return 0;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

int maxDepth = 0;
while (!queue.isEmpty()) {
int queueSize = queue.size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queue のサイズをあらかじめ取得し、そのサイズ分だけ処理をするという処理は、普段見ないように思います。 queue の中に TreeNode と 深さの両方を入れるか、現在処理中のキューと、次に処理予定のキューに分けるほうが一般的だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

木構造の階層ごとに処理を挟みたいからこの処理にしています。
Pairで深さを入れる方が良さそうですね。

for (int i = 0; i < queueSize; i++) {
TreeNode curr = queue.poll();
if (curr.left != null) {
queue.add(curr.left);
}
if (curr.right != null) {
queue.add(curr.right);
}
}
maxDepth++;
}

return maxDepth;
}
}
```
41 changes: 41 additions & 0 deletions MaximumDepthOfBinaryTree/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
```java
class Solution {
public int maxDepth(TreeNode root) {
return maxDepthFindByBfs(root);
}

public int maxDepthFindByDfs(TreeNode root) {
if (root == null) {
return 0;
}

return Math.max(maxDepthFindByDfs(root.left), maxDepthFindByDfs(root.right)) + 1;
}

// BFS
// Time spend: 04:30
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}

Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>();
queue.add(new Pair(root, 1));

int maxDepth = 1;
while (!queue.isEmpty()) {
Pair<TreeNode, Integer> current = queue.poll();
maxDepth = Math.max(current.getValue(), maxDepth);

if (current.getKey().left != null) {
queue.add(new Pair(current.getKey().left, current.getValue() + 1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pair の使用は極力避けましょう。理由は、 key と value に何が含まれているかをソースコードから読み取るのにやや認知負荷がかかるためです。

class TreeNodeAndDepth {
    public TreeNode treeNode;
    public int depth;
}

等、ユーザー型を定義し、それに入れたほうが読みやすいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

プロダクトに使うことでしたらそうしますが、面接の時は定義の実装少し面倒なため、一言普段こうやって書きませんよってやんわり流しながらPair使うのは許されるものでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、大丈夫だと思います。

}
if (current.getKey().right != null) {
queue.add(new Pair(current.getKey().right, current.getValue() + 1));
}
}

return maxDepth;
}
}
```
55 changes: 55 additions & 0 deletions MaximumDepthOfBinaryTree/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```java
// Time spend: 02:11
class Solution {
public int maxDepth(TreeNode root) {
return maxDepthByDfs(root, 1);
}

public int maxDepthByDfs(TreeNode root, int currentDepth) {
if (root == null) {
return currentDepth - 1;
}

int nextDepth = currentDepth + 1;
return Math.max(maxDepthByDfs(root.left, nextDepth),
maxDepthByDfs(root.right, nextDepth));
}
}
```

```java
// Time spend: 02:03
class Solution {
public int maxDepth(TreeNode root) {
return maxDepthByDfs(root, 1);
}

public int maxDepthByDfs(TreeNode root, int currentDepth) {
if (root == null) {
return currentDepth - 1;
}

int nextDepth = currentDepth + 1;
return Math.max(maxDepthByDfs(root.left, nextDepth),
maxDepthByDfs(root.right, nextDepth));
}
}
```

```java
// Time spend: 01:42
class Solution {
public int maxDepth(TreeNode root) {
return maxDepthByDfs(root, 1);
}

public int maxDepthByDfs(TreeNode root, int currentDepth) {
if (root == null) {
return currentDepth - 1;
}

int nextDepth = currentDepth + 1;
return Math.max(maxDepthByDfs(root.left, nextDepth), maxDepthByDfs(root.right, nextDepth));
}
}
```