Commit 3e4e27dd by yaozc

### Tuesday night. Keep going!!!

parent d00e3a5a
 ... ... @@ -470,13 +470,98 @@ public: } res.push_back(temp); } return res; } }; Binary Tree Zigzag Level Order Traversal //和上一题BFS不同的是，zigzag使用的是2个stack，奇数层用s1，偶数层用s2。 // 奇数层先看left sub tree,然后right sub tree. 偶数层先看right sub tree, 然后left sub tree // https://www.youtube.com/watch?v=YsLko6sSKh8&t=49s /** * 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> zigzagLevelOrder(TreeNode* root) { vector> res; if(!root) return res; stack s1; stack s2; s1.push(root); int level = 0; while( !s1.empty()|| !s2.empty()) { if(level%2 == 0){ vector temp; while(!s1.empty()){ cout<<"1"<val); if(curr->left) s2.push(curr->left); if(curr->right) s2.push(curr->right); } res.push_back(temp); } else{ vector temp; while(!s2.empty()){ cout<<"2"<val); if(curr->right) s1.push(curr->right); if(curr->left) s1.push(curr->left); } res.push_back(temp); } level++; cout<<"3"<left), 0); int right = max(max_gain(parent->right), 0); int new_path = left + right + parent->val; sum = max(sum, new_path); return max(parent->val + max(left, right), 0); } }; ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!