日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode 关于树的题目

發(fā)布時(shí)間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 关于树的题目 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

判斷一棵樹里是否有兩個(gè)節(jié)點(diǎn)的值之和等于某個(gè)值。

653.?Two Sum IV - Input is a BST

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 1:

Input: 5/ \3 6/ \ \ 2 4 7Target = 9Output: True

?

Example 2:

Input: 5/ \3 6/ \ \ 2 4 7Target = 28Output: False
思路:使用 unordered_set存儲(chǔ)?節(jié)點(diǎn)的值。 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { private:unordered_set<int> s;bool dfs(TreeNode* root,int k, unordered_set<int>& s){if(root==nullptr) return false;if(s.count(k-root->val)) return true;s.insert(root->val);return dfs(root->left,k,s)||dfs(root->right,k,s);} public:bool findTarget(TreeNode* root, int k) {s.clear();return dfs(root,k,s);} };

?python代碼

創(chuàng)建集合 set(), 插入 add (c++ insert)

# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = Noneclass Solution(object):def helper(self,root,k,s):if not root:return Falseif k-root.val in s:return Trues.add(root.val)return self.helper(root.left,k,s) or self.helper(root.right,k,s)def findTarget(self, root, k):""":type root: TreeNode:type k: int:rtype: bool"""s=set()return self.helper(root,k,s)

?606.?Construct String from Binary Tree

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

Example 1:

Input: Binary tree: [1,2,3,4]1/ \2 3/ 4 Output: "1(2(4))(3)"
Explanation: Originallay it needs to be "1(2(4)())(3()())",
but you need to omit all the unnecessary empty parenthesis pairs.
And it will be "1(2(4))(3)".

?

Example 2:

Input: Binary tree: [1,2,3,null,4]1/ \2 3\ 4 Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.

?

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { public:string tree2str(TreeNode* t) {if(t==nullptr)return "";string s=to_string(t->val);if(t->left==nullptr){if(t->right==nullptr)return s;else{return s+"()"+"("+tree2str(t->right)+")";}}else{return s+"("+tree2str(t->left)+")"+(!t->right?"":"("+tree2str(t->right)+")");}} }; # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = Noneclass Solution(object):def tree2str(self, t):""":type t: TreeNode:rtype: str"""if not t:return ""if not t.left:return str(t.val)+("()"+"("+self.tree2str(t.right)+")" if t.right else "")else:return str(t.val)+"("+self.tree2str(t.left)+")"+("("+self.tree2str(t.right)+")" if t.right else "")

?

轉(zhuǎn)載于:https://www.cnblogs.com/learning-c/p/9280596.html

總結(jié)

以上是生活随笔為你收集整理的leetcode 关于树的题目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。