国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

226. Invert Binary Tree/112. Path Sum

2019-11-08 03:26:15
字體:
來源:轉載
供稿:網友

Invert Binary Tree題目描述代碼實現Path Sum題目描述代碼實現

226. Invert Binary Tree

題目描述

Invert a binary tree.

4 / / 2 7 / / / /1 3 6 9

to

4 / / 7 2 / / / /9 6 3 1

Trivia: This PRoblem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

代碼實現

/** * 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: TreeNode* invertTree(TreeNode* root) { if(!root) return root; TreeNode *l = root->left; root->left = root->right; root->right = l; if(root->left) invertTree(root->left); if(root->right) invertTree(root->right); return root; }};

更加簡潔的寫法:

class Solution {public: TreeNode* invertTree(TreeNode* root) { if (root) { invertTree(root->left); invertTree(root->right); std::swap(root->left, root->right); } return root; }};

使用廣度優先的算法:

class Solution {public: TreeNode* invertTree(TreeNode* root) { std::stack<TreeNode*> stk; stk.push(root); while (!stk.empty()) { TreeNode* p = stk.top(); stk.pop(); if (p) { stk.push(p->left); stk.push(p->right); std::swap(p->left, p->right); } } return root; }};

112. Path Sum

題目描述

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.

代碼實現

這種方法是使用了DFS。

class Solution {public: bool isEqual(TreeNode* root, int sum, int leafsum) { if(root == NULL) return false; leafsum += root->val; if(sum == leafsum && !root->left && !root->right) return true; return isEqual(root->left, sum, leafsum) || isEqual(root->right, sum, leafsum); } bool haspathSum(TreeNode* root, int sum) { if(!root) return false; int leafsum = 0; return isEqual(root, sum, leafsum); }};

當然使用BFS也是可以的。 在leetcode的discussion部分,里面有的是BFS的做法。這里有兩個實現: 其中一個做法不需要額外的數據結構在記錄整個過程中的sum:

class Solution {public: bool hasPathSum(TreeNode* root, int sum) { stack<TreeNode*> st; TreeNode* itr = root; while(itr != NULL || !st.empty()){ while(itr != NULL){ if(itr->right != NULL) st.push(itr->right); st.push(itr); sum -= itr->val; itr = itr->left; } TreeNode* temp = st.top(); st.pop(); if(temp->left == NULL && temp->right == NULL && sum == 0) return true; if(temp->right && !st.empty() && temp->right == st.top()){ itr = st.top(); st.pop(); st.push(temp); }else{ sum += temp->val; } } return false; }};

另外一種BFS更容易理解,使用的是普通的廣度優先策略,用pair記錄節點以及當前的和。

bool hasPathSum1(TreeNode* root, int sum) { if(root==nullptr) return false; vector<pair<TreeNode*, int>> stack; auto cur=make_pair(root, root->val); while(cur.first || stack.size()){ while(cur.first){ stack.push_back(cur); if(cur.first->left==nullptr) cur=make_pair(cur.first->left, 0); else cur=make_pair(cur.first->left, cur.second+cur.first->left->val); } auto node=stack.back(); stack.pop_back(); if(node.first->left==nullptr && node.first->right==nullptr) if(node.second==sum) return true; if(node.first->right) cur=make_pair(node.first->right, node.second+node.first->right->val); } return false;}

在這里我覺得廣度優先和深度優先各有優勢,廣度優先適合比較短的路徑,而深度優先適合更加長的,窄的路徑。

參考鏈接:

https://discuss.leetcode.com/topic/41568/comparison-of-c-non-recursive-solutions


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安庆市| 郁南县| 吉林省| 阳朔县| 崇明县| 宜丰县| 武乡县| 获嘉县| 楚雄市| 玉林市| 浦县| 桐庐县| 屏边| 怀集县| 阿坝县| 荣成市| 荃湾区| 新乡市| 精河县| 洪湖市| 开远市| 尉氏县| 中山市| 邹平县| 溧水县| 斗六市| 蒲城县| 玛曲县| 岑巩县| 黑河市| 油尖旺区| 监利县| 顺义区| 定结县| 江阴市| 开封市| 古田县| 确山县| 鲁甸县| 哈巴河县| 通道|