国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Two Sum IV - Input is a BST

snifes / 2230人閱讀

Problem

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example

Example 1:
Input:

    5
   / 
  3   6
 /    
2   4   7

Target = 9

Output: True
Example 2:
Input:

    5
   / 
  3   6
 /    
2   4   7

Target = 28

Output: False

Solution
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean findTarget(TreeNode root, int k) {
        Queue queue = new LinkedList();
        Set set = new HashSet<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (set.contains(k-cur.val)) return true;
            set.add(cur.val);
            if (cur.left != null) queue.offer(cur.left);
            if (cur.right != null) queue.offer(cur.right);
        }
        return false;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/69362.html

相關(guān)文章

  • Leetcode PHP題解--D89 653. Two Sum IV - Input is a B

    摘要:思路思路遍歷的時(shí)候,先把節(jié)點(diǎn)存起來,并且與每一個(gè)值相加,判斷是否等于所需值。用函數(shù)判斷與所求數(shù)字之差是否在數(shù)組內(nèi)。否則,遍歷子節(jié)點(diǎn)。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 D89 653. Two Sum IV - Input is a BST 題目鏈接 653. Two Sum IV - Input is a BST 題目分析 給定一個(gè)二叉樹以及一個(gè)目標(biāo)數(shù)字,判斷能不能通過...

    HtmlCssJs 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • 11.leetcode Range Sum of BST

    1. Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. #### 1. 例子 Inp...

    shiweifu 評(píng)論0 收藏0
  • [LeetCode] 776. Split BST

    Problem Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, whil...

    baiy 評(píng)論0 收藏0
  • LeetCode 272 Closest Binary Tree Traversal II 解題思路

    摘要:原題網(wǎng)址題意在二叉搜索樹當(dāng)中找到離最近的個(gè)數(shù)。解題思路由于二叉搜索數(shù)的中序遍歷是有序的,比如例子中的樹,中序遍歷為。 原題網(wǎng)址:https://leetcode.com/problems... Given a non-empty binary search tree and a target value, find?k?values in the BST that are closes...

    Youngdze 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<