From 827bf9f2ed20d4c647561fe9a0d73b761d04564e Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Mon, 15 Apr 2024 02:49:52 +0900 Subject: [PATCH] add: Maximum Depth Of Binary Tree --- MaximumDepthOfBinaryTree/step1.md | 43 ++++++++++++++++++++++++ MaximumDepthOfBinaryTree/step2.md | 41 +++++++++++++++++++++++ MaximumDepthOfBinaryTree/step3.md | 55 +++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 MaximumDepthOfBinaryTree/step1.md create mode 100644 MaximumDepthOfBinaryTree/step2.md create mode 100644 MaximumDepthOfBinaryTree/step3.md diff --git a/MaximumDepthOfBinaryTree/step1.md b/MaximumDepthOfBinaryTree/step1.md new file mode 100644 index 0000000..fa3237d --- /dev/null +++ b/MaximumDepthOfBinaryTree/step1.md @@ -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 queue = new LinkedList<>(); + queue.add(root); + + int maxDepth = 0; + while (!queue.isEmpty()) { + int queueSize = queue.size(); + 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; + } +} +``` \ No newline at end of file diff --git a/MaximumDepthOfBinaryTree/step2.md b/MaximumDepthOfBinaryTree/step2.md new file mode 100644 index 0000000..77a0b39 --- /dev/null +++ b/MaximumDepthOfBinaryTree/step2.md @@ -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> queue = new LinkedList<>(); + queue.add(new Pair(root, 1)); + + int maxDepth = 1; + while (!queue.isEmpty()) { + Pair current = queue.poll(); + maxDepth = Math.max(current.getValue(), maxDepth); + + if (current.getKey().left != null) { + queue.add(new Pair(current.getKey().left, current.getValue() + 1)); + } + if (current.getKey().right != null) { + queue.add(new Pair(current.getKey().right, current.getValue() + 1)); + } + } + + return maxDepth; + } +} +``` \ No newline at end of file diff --git a/MaximumDepthOfBinaryTree/step3.md b/MaximumDepthOfBinaryTree/step3.md new file mode 100644 index 0000000..279f93b --- /dev/null +++ b/MaximumDepthOfBinaryTree/step3.md @@ -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)); + } +} +``` \ No newline at end of file