leetcode : Binary Tree Paths

來源:互聯網
上載者:User

標籤:

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

 

   1 /   2     3   5

 

All root-to-leaf paths are:

["1->2->5", "1->3"]

 

思路:用兩個stack<TreeNode*> in , s;

in : 記錄當前的路徑 p  , 和vector<int>path 相同,只不過一個記錄的是 val ,一個記錄點的指標。

s  : 記錄每個節點的  p->right 

v2s : 將path轉換稱符合要求的string

/** * 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 v2s(vector<int>a){    const int len = a.size();    string re = "";    char *p = new char[500];    for (int i = 0; i<len; i++){        stringstream ss;    // 需要包含標頭檔  #include<sstream>        string temp;        ss << a[i];        ss >> temp;        re = re + temp;        if (i != len - 1)            re = re + "->";    }    return re;}vector<string> binaryTreePaths(TreeNode* root) {    vector<int> path;    vector<string> re;    if (root == NULL) return re;    TreeNode * p = root;    stack<TreeNode*> in, s;    while (p != NULL){        if (p->left == NULL&&p->right == NULL){            in.push(p);            path.push_back(p->val);            re.push_back(v2s(path));            if (s.empty())                return re;            else {
          // 通過while迴圈,尋找下一個需要遍曆的點
while (!in.empty() && !s.empty() && (in.top())->right != s.top()){ in.pop(); path.erase(path.end()-1); } p = s.top(); s.pop(); } } else if (p->left == NULL&&p->right != NULL){ path.push_back(p->val); in.push(p); p = p->right; } else if (p->left != NULL&&p->right == NULL){ path.push_back(p->val); in.push(p); p = p->left; } else{ path.push_back(p->val); in.push(p); s.push(p->right); p = p->left; } } return re;}};

 另一道相似題目

有一棵二叉樹,樹上每個點標有權值,權值各不相同,請設計一個演算法算出權值最大的分葉節點到權值最小的分葉節點的距離。二叉樹每條邊的距離為1,一個節點經過多少條邊到達另一個節點為這兩個節點之間的距離。

給定二叉樹的根節點root,請返回所求距離。

思路:只需要將儲存的路徑進行比較,相同的去掉然後相加,就能算出路徑長度。

/*struct TreeNode {    int val;    struct TreeNode *left;    struct TreeNode *right;    TreeNode(int x) :            val(x), left(NULL), right(NULL) {    }};*/class Tree {public:int getDis(TreeNode* root) {// write code herestack<TreeNode*> max, min;int m = INT_MIN, n = INT_MAX, re = 0;stack<TreeNode*> in, s;TreeNode *p = root;while (p != NULL){if (p->left == NULL&&p->right == NULL){in.push(p);if (p->val > m){max = in;m = p->val;}if (p->val < n){min = in;n = p->val;}while (!in.empty() && !s.empty() && (in.top())->right != s.top()){in.pop();}if (s.empty())break;else {p = s.top();s.pop();}}else if (p->left == NULL&&p->right != NULL){in.push(p);p = p->right;}else if (p->left != NULL&&p->right == NULL){in.push(p);p = p->left;}else {in.push(p);s.push(p->right);p = p->left;}}stack<TreeNode*> t1, t2;while (!max.empty()){t1.push(max.top());max.pop();}while (!min.empty()){t2.push(min.top());min.pop();}while (!t1.empty() && !t2.empty()){if (t1.top() != t2.top())break;elset1.pop(); t2.pop();}re = re + t1.size() + t2.size();return re;}};

  

 

leetcode : Binary Tree Paths

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.