|
| 1 | +## :pencil2:Leetcode之PHP版题目解析(221. Maximal Square) |
| 2 | +**2020-05-18 吴亲库里 库里的深夜食堂** |
| 3 | +**** |
| 4 | +### :pencil2:描述 |
| 5 | +**给定一棵完全二叉树,求此完全二叉树的节点个数** |
| 6 | +**** |
| 7 | +### :pencil2:题目实例 |
| 8 | +<a href="https://github.com/wuqinqiang/"> |
| 9 | + <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/222.png"> |
| 10 | +</a> |
| 11 | +**** |
| 12 | + |
| 13 | +### :pencil2:题目分析 |
| 14 | +**可以使用递归一个个的计算节点,一行代码干净,但是时间复杂度在O(n),需要全遍历一遍** |
| 15 | + |
| 16 | +### :pencil2:代码实现1 |
| 17 | + |
| 18 | +```php |
| 19 | + /** |
| 20 | + * @param TreeNode $root |
| 21 | + * @return Integer |
| 22 | + */ |
| 23 | + function countNodes($root) { |
| 24 | + return $root !=null ?1+$this->countNodes($root->left)+$this->countNodes($root->right):0; |
| 25 | + } |
| 26 | +``` |
| 27 | +**** |
| 28 | + |
| 29 | +**再想想,题目给的是完全二叉树,完全二叉树的特点是什么?除了最后一层(暂且称之为K),其他每层的节点都是满的,并且最后一层的节点全部靠左** |
| 30 | +**也就是说我们其实是在求最后一层的节点数是多少,因为前面0到K-1层的节点是满的,那么利用此树的特性,除最后一层外的所有节点数就是2^d-1(d表示树的高度),对于最后一层我们可以通过二分查找的方式解决,因为最后一层都是靠左的,我只要检查第index 个节点存不存在,存在就向右靠,不存在即右边已经没有了,像左靠.具体看代码** |
| 31 | + |
| 32 | +### :pencil2:代码实现1 |
| 33 | +```php |
| 34 | +/** |
| 35 | + * Definition for a binary tree node. |
| 36 | + * class TreeNode { |
| 37 | + * public $val = null; |
| 38 | + * public $left = null; |
| 39 | + * public $right = null; |
| 40 | + * function __construct($val = 0, $left = null, $right = null) { |
| 41 | + * $this->val = $val; |
| 42 | + * $this->left = $left; |
| 43 | + * $this->right = $right; |
| 44 | + * } |
| 45 | + * } |
| 46 | + */ |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +class Solution { |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * @param TreeNode $root |
| 56 | + * @return Integer |
| 57 | + */ |
| 58 | + function countNodes($root) { |
| 59 | + if(!$root) return 0; |
| 60 | + $dep=$this->depthNodes($root); |
| 61 | + if($dep==0) return 1; |
| 62 | + $left=1;$right=pow(2,$dep)-1; |
| 63 | + while($left<=$right){ |
| 64 | + $middle=$left+floor(($right-$left)/2); |
| 65 | + if($this->hasNode($middle,$dep,$root)){ |
| 66 | + $left=$middle+1; |
| 67 | + }else{ |
| 68 | + $right=$middle-1; |
| 69 | + } |
| 70 | + } |
| 71 | + return pow(2,$dep)-1+$left; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + function depthNodes($node) |
| 76 | + { |
| 77 | + $dep=0; |
| 78 | + while($node->left !=null){ |
| 79 | + $node=$node->left; |
| 80 | + ++$dep; |
| 81 | + } |
| 82 | + return $dep; |
| 83 | + } |
| 84 | + |
| 85 | + function hasNode($middle,$d,$node) |
| 86 | + { |
| 87 | + $left=0;$right=pow(2,$d)-1; |
| 88 | + for($i=0;$i<$d;++$i){ |
| 89 | + $pow=$left+floor(($right-$left)/2); |
| 90 | + if($middle<=$pow){ |
| 91 | + $node=$node->left; |
| 92 | + $right=$pow; |
| 93 | + }else{ |
| 94 | + $node=$node->right; |
| 95 | + $left=$pow+1; |
| 96 | + } |
| 97 | + } |
| 98 | + return $node !=null; |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### 联系 |
| 104 | + |
| 105 | +<a href="https://github.com/wuqinqiang/"> |
| 106 | + <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px"> |
| 107 | +</a> |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments