Skip to content

Commit 39fcf21

Browse files
authored
Added tasks 101-136
1 parent 8fc2be1 commit 39fcf21

File tree

11 files changed

+760
-0
lines changed

11 files changed

+760
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225

226226
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
227227
|-|-|-|-|-|-
228+
| 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99
228229

229230
#### Day 8
230231

@@ -353,6 +354,7 @@
353354

354355
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
355356
|-|-|-|-|-|-
357+
| 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 9 | 63.06
356358

357359
#### Day 11 Containers and Libraries
358360

@@ -620,11 +622,13 @@
620622

621623
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
622624
|-|-|-|-|-|-
625+
| 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99
623626

624627
#### Day 6 Tree
625628

626629
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
627630
|-|-|-|-|-|-
631+
| 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08
628632

629633
#### Day 7 Binary Search
630634

@@ -756,6 +760,7 @@
756760

757761
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
758762
|-|-|-|-|-|-
763+
| 0101 |[Symmetric Tree](src/main/php/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 6 | 76.40
759764

760765
#### Day 16 Design
761766

@@ -791,6 +796,7 @@
791796

792797
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
793798
|-|-|-|-|-|-
799+
| 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08
794800
| 0007 |[Reverse Integer](src/main/php/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 3 | 90.99
795801
| 0009 |[Palindrome Number](src/main/php/g0001_0100/s0009_palindrome_number)| Easy | Math | 23 | 76.73
796802

@@ -813,6 +819,7 @@
813819

814820
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
815821
|-|-|-|-|-|-
822+
| 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99
816823
| 0001 |[Two Sum](src/main/php/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 9 | 97.47
817824
| 0055 |[Jump Game](src/main/php/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 129 | 74.56
818825
| 0075 |[Sort Colors](src/main/php/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 88.78
@@ -849,6 +856,7 @@
849856

850857
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
851858
|-|-|-|-|-|-
859+
| 0114 |[Flatten Binary Tree to Linked List](src/main/php/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 3 | 100.00
852860
| 0024 |[Swap Nodes in Pairs](src/main/php/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 3 | 85.71
853861
| 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57
854862
| 0025 |[Reverse Nodes in k-Group](src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 3 | 100.00
@@ -858,6 +866,9 @@
858866
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
859867
|-|-|-|-|-|-
860868
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09
869+
| 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08
870+
| 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 9 | 63.06
871+
| 0124 |[Binary Tree Maximum Path Sum](src/main/php/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 16 | 100.00
861872
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 10 | 70.97
862873

863874
#### Udemy Trie and Heap
@@ -917,6 +928,7 @@
917928

918929
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
919930
|-|-|-|-|-|-
931+
| 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99
920932

921933
#### Day 4 Array
922934

@@ -961,6 +973,9 @@
961973

962974
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
963975
|-|-|-|-|-|-
976+
| 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08
977+
| 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 9 | 63.06
978+
| 0101 |[Symmetric Tree](src/main/php/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 6 | 76.40
964979

965980
#### Day 12 Tree
966981

@@ -984,6 +999,7 @@
984999

9851000
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9861001
|-|-|-|-|-|-
1002+
| 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08
9871003
| 0015 |[3Sum](src/main/php/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1), Big_O_Time_O(n\*log(n))_Space_O(n^2) | 252 | 77.94
9881004

9891005
#### Day 2 Array
@@ -1063,6 +1079,7 @@
10631079

10641080
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10651081
|-|-|-|-|-|-
1082+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/php/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 14 | 63.33
10661083

10671084
#### Day 16 Tree
10681085

@@ -1171,6 +1188,7 @@
11711188

11721189
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11731190
|-|-|-|-|-|-
1191+
| 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08
11741192

11751193
### Algorithm II
11761194

@@ -1298,6 +1316,16 @@
12981316

12991317
| # | Title | Difficulty | Tag | Time, ms | Time, %
13001318
|------|----------------|-------------|-------------|----------|--------
1319+
| 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08
1320+
| 0131 |[Palindrome Partitioning](src/main/php/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 159 | 61.90
1321+
| 0128 |[Longest Consecutive Sequence](src/main/php/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 154 | 75.61
1322+
| 0124 |[Binary Tree Maximum Path Sum](src/main/php/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 16 | 100.00
1323+
| 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99
1324+
| 0114 |[Flatten Binary Tree to Linked List](src/main/php/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 3 | 100.00
1325+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/php/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 14 | 63.33
1326+
| 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 9 | 63.06
1327+
| 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08
1328+
| 0101 |[Symmetric Tree](src/main/php/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 6 | 76.40
13011329
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(log(N)) | 10 | 70.97
13021330
| 0096 |[Unique Binary Search Trees](src/main/php/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
13031331
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 101\. Symmetric Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
13+
14+
**Input:** root = [1,2,2,3,4,4,3]
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg)
21+
22+
**Input:** root = [1,2,2,null,3,null,3]
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is in the range `[1, 1000]`.
29+
* `-100 <= Node.val <= 100`
30+
31+
**Follow up:** Could you solve it both recursively and iteratively?
32+
33+
## Solution
34+
35+
```php
36+
/**
37+
* Definition for a binary tree node.
38+
* class TreeNode {
39+
* public $val = null;
40+
* public $left = null;
41+
* public $right = null;
42+
* function __construct($val = 0, $left = null, $right = null) {
43+
* $this->val = $val;
44+
* $this->left = $left;
45+
* $this->right = $right;
46+
* }
47+
* }
48+
*/
49+
class Solution {
50+
/**
51+
* @param TreeNode $root
52+
* @return Boolean
53+
*/
54+
function isSymmetric($root) {
55+
if ($root == null) {
56+
return true;
57+
}
58+
return $this->helper($root->left, $root->right);
59+
}
60+
61+
private function helper($leftNode, $rightNode) {
62+
if ($leftNode == null || $rightNode == null) {
63+
return $leftNode == null && $rightNode == null;
64+
}
65+
if ($leftNode->val != $rightNode->val) {
66+
return false;
67+
}
68+
return $this->helper($leftNode->left, $rightNode->right) && $this->helper($leftNode->right, $rightNode->left);
69+
}
70+
}
71+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 102\. Binary Tree Level Order Traversal
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
13+
14+
**Input:** root = [3,9,20,null,null,15,7]
15+
16+
**Output:** [[3],[9,20],[15,7]]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1]
21+
22+
**Output:** [[1]]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 2000]`.
33+
* `-1000 <= Node.val <= 1000`
34+
35+
## Solution
36+
37+
```php
38+
/**
39+
* Definition for a binary tree node.
40+
* class TreeNode {
41+
* public $val = null;
42+
* public $left = null;
43+
* public $right = null;
44+
* function __construct($val = 0, $left = null, $right = null) {
45+
* $this->val = $val;
46+
* $this->left = $left;
47+
* $this->right = $right;
48+
* }
49+
* }
50+
*/
51+
class Solution {
52+
/**
53+
* @param TreeNode $root
54+
* @return Integer[][]
55+
*/
56+
function levelOrder($root) {
57+
$result = array();
58+
if ($root == null) {
59+
return $result;
60+
}
61+
$queue = new \SplQueue();
62+
$queue->enqueue($root);
63+
$queue->enqueue(null);
64+
$level = array();
65+
while (!$queue->isEmpty()) {
66+
$root = $queue->dequeue();
67+
while (!$queue->isEmpty() && $root != null) {
68+
array_push($level, $root->val);
69+
if ($root->left != null) {
70+
$queue->enqueue($root->left);
71+
}
72+
if ($root->right != null) {
73+
$queue->enqueue($root->right);
74+
}
75+
$root = $queue->dequeue();
76+
}
77+
array_push($result, $level);
78+
$level = array();
79+
if (!$queue->isEmpty()) {
80+
$queue->enqueue(null);
81+
}
82+
}
83+
return $result;
84+
}
85+
}
86+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 104\. Maximum Depth of Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _its maximum depth_.
9+
10+
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
15+
16+
**Input:** root = [3,9,20,null,null,15,7]
17+
18+
**Output:** 3
19+
20+
**Example 2:**
21+
22+
**Input:** root = [1,null,2]
23+
24+
**Output:** 2
25+
26+
**Example 3:**
27+
28+
**Input:** root = []
29+
30+
**Output:** 0
31+
32+
**Example 4:**
33+
34+
**Input:** root = [0]
35+
36+
**Output:** 1
37+
38+
**Constraints:**
39+
40+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
41+
* `-100 <= Node.val <= 100`
42+
43+
## Solution
44+
45+
```php
46+
/**
47+
* Definition for a binary tree node.
48+
* class TreeNode {
49+
* public $val = null;
50+
* public $left = null;
51+
* public $right = null;
52+
* function __construct($val = 0, $left = null, $right = null) {
53+
* $this->val = $val;
54+
* $this->left = $left;
55+
* $this->right = $right;
56+
* }
57+
* }
58+
*/
59+
class Solution {
60+
/**
61+
* @param TreeNode $root
62+
* @return Integer
63+
*/
64+
function maxDepth($root) {
65+
return $this->findDepth($root, 0);
66+
}
67+
68+
private function findDepth($node, $currentDepth) {
69+
if ($node == null) {
70+
return 0;
71+
}
72+
$currentDepth++;
73+
return 1
74+
+ max($this->findDepth($node->left, $currentDepth), $this->findDepth($node->right, $currentDepth));
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)