leetcode653. Two Sum IV
来源:互联网 发布:ddos日志分析算法 编辑:程序博客网 时间:2024/05/21 22:42
题意是求二叉树中的任意两个数的和能不能是k。
把二叉树便利一遍,求出所有元素,然后在循环便利每两个的和就好。
/** * 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: vector<int> s; void f(TreeNode* root){ if(root!=NULL) s.push_back(root->val); if(root->left!=NULL) f(root->left); if(root->right!=NULL) f(root->right); } bool findTarget(TreeNode* root, int k) { f(root); int n=s.size(); for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(s[i]+s[j]==k) return 1; } } return 0; }};
阅读全文
0 0
- Leetcode653. Two Sum IV
- leetcode653. Two Sum IV
- LeetCode653. Two Sum IV
- LeetCode653. Two Sum IV
- python--leetcode653. Two Sum IV
- 653. Two Sum IV
- 653. Two Sum IV
- 653. Two Sum IV
- Two Sum IV
- leetcode[Two Sum IV
- LeetCode Two Sum IV
- 653. Two Sum IV
- Two Sum IV
- 653. Two Sum IV
- 653. Two Sum IV
- 653. Two Sum IV
- leetcode Two Sum IV
- Two Sum IV
- B. New Year Present----构造
- SpringBoot详解(一)-快速入门
- 小米路由器青春版刷Padavan固件
- Unity LLAPI 底层Native插件在x86 (32bit) 下编译的一点注意
- HDU 4911 归并排序求逆序对
- leetcode653. Two Sum IV
- C语言中内存分配
- recv_MSG_peek
- JVM--对象的创建
- Pat(A) 1085. Perfect Sequence (25)
- Linux权限管理(基本权限、默认权限)
- python 批量升级所有库
- Codeforces Round #430 Vitya and Strange Lesson 逆向思维+01trie
- 黑客与画家读书笔记