Leetcode PHP题解--D89 653. Two Sum IV - Input is a BST

栏目: PHP · 发布时间: 7年前

内容简介:给定一个二叉树以及一个目标数字,判断能不能通过二叉树中任意两个节点的值相加得到。遍历的时候,先把节点存起来,并且与每一个值相加,判断是否等于所需值。

D89 653. Two Sum IV - Input is a BST

题目链接

653. Two Sum IV - Input is a BST

题目分析

给定一个二叉树以及一个目标数字,判断能不能通过二叉树中任意两个节点的值相加得到。

思路

思路1

遍历的时候,先把节点存起来,并且与每一个值相加,判断是否等于所需值。

这个算法很明显效率比较低。

思路2

遍历的时候,把自身作为键存进数组内。用isset函数判断与所求数字之差是否在数组内。存在既返回。否则,遍历子节点。

最终代码

<?php
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */
class Solution {
    protected $has = [];
        protected $hasResult = false;
            /**
                 * @param TreeNode $root
                      * @param Integer $k
                           * @return Boolean
                                */
                                    function findTarget($root, $k) {
                                            if(is_null($root)){
                                                        return;
                                                                }
                                                                        if(isset($this->has[$k-($root->val)])){
                                                                                    $this->hasResult = true;
                                                                                                return $this->hasResult;
                                                                                                        }
                                                                                                                else{
                                                                                                                            $this->has[$root->val] = true;
                                                                                                                                        if(!$this->findTarget($root->left, $k)){
                                                                                                                                                        $this->findTarget($root->right, $k);
                                                                                                                                                                    }
                                                                                                                                                                            }
                                                                                                                                                                                    return $this->hasResult;
                                                                                                                                                                                        }
                                                                                                                                                                                        }
若觉得本文章对你有用,欢迎用[爱发电](https://afdian.net/@skys215)资助。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical Vim, Second Edition

Practical Vim, Second Edition

Drew Neil / The Pragmatic Bookshelf / 2015-10-31 / USD 29.00

Vim is a fast and efficient text editor that will make you a faster and more efficient developer. It’s available on almost every OS, and if you master the techniques in this book, you’ll never need an......一起来看看 《Practical Vim, Second Edition》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具