[LeetCode]112.Path Sum,leetcode112.path

來源:互聯網
上載者:User

[LeetCode]112.Path Sum,leetcode112.path
【題目】

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.


【分析】

題目要求是從根節點到葉子節點的一條路徑。剛開始沒看清楚,還以為隨意路徑。

題目只要求返回true或者false,因此沒有必要記錄路徑。

【代碼】

/**********************************   日期:2015-01-01*   作者:SJF0115*   題目: 112.Path Sum*   來源:https://oj.leetcode.com/problems/path-sum/*   結果:AC*   來源:LeetCode*   部落格:*   時間複雜度:O(n)*   空間複雜度:O(logn)**********************************/#include <iostream>#include <queue>#include <vector>using namespace std;// 二叉樹節點struct TreeNode {    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:    bool hasPathSum(TreeNode *root, int sum) {        if (root == NULL){            return false;        }//if        // 找到一條從根節點到葉子節點的路徑        if(root->left == NULL && root->right == NULL){            return sum == root->val;        }//if        // 左子樹        bool left = hasPathSum(root->left,sum - root->val);        // 右子樹        bool right = hasPathSum(root->right,sum - root->val);        return left || right;    }};// 建立二叉樹TreeNode* CreateTreeByLevel(vector<int> num){    int len = num.size();    if(len == 0){        return NULL;    }//if    queue<TreeNode*> queue;    int index = 0;    // 建立根節點    TreeNode *root = new TreeNode(num[index++]);    // 入隊列    queue.push(root);    TreeNode *p = NULL;    while(!queue.empty() && index < len){        // 出隊列        p = queue.front();        queue.pop();        // 左節點        if(index < len && num[index] != -1){            // 如果不空建立一個節點            TreeNode *leftNode = new TreeNode(num[index]);            p->left = leftNode;            queue.push(leftNode);        }        index++;        // 右節點        if(index < len && num[index] != -1){            // 如果不空建立一個節點            TreeNode *rightNode = new TreeNode(num[index]);            p->right = rightNode;            queue.push(rightNode);        }        index++;    }//while    return root;}int main() {    Solution solution;    // -1代表NULL    vector<int> num = {5,4,8,11,-1,13,4,7,2,-1,-1,-1,1};    TreeNode* root = CreateTreeByLevel(num);    cout<<solution.hasPathSum(root,22)<<endl;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.