-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion.php
43 lines (36 loc) · 940 Bytes
/
Recursion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution {
private $solution = [];
/**
* @param TreeNode $root
* @return Integer[][]
*/
function levelOrder($root) {
$this->walk($root, 0);
return $this->solution;
}
/**
* @param TreeNode $root
* @return void
*/
function walk($node, $depth){
if ($node == null) return ;
if(!isset($this->solution[$depth])) $this->solution[$depth] = [];
array_push($this->solution[$depth], $node->val) ;
$this->walk($node->left, ($depth + 1));
$this->walk($node->right, ($depth + 1));
}
}