[Leetcode] 102, 107, 101
来源:互联网 发布:软件项目验收流程 编辑:程序博客网 时间:2024/06/07 18:57
102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7]]
Solution: 先序遍历或者后序遍历都行,遍历的同时记录一下深度就可以了
Code(递归版):
/** * 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<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> ans; travel(root, 0, ans); return ans; }private: void travel(TreeNode* root, int depth, vector<vector<int>>& m){ if(root==NULL) return; if(depth>=m.size()) m.push_back(vector<int>()); travel(root->left, depth+1, m); travel(root->right, depth+1, m); m[depth].push_back(root->val); }};
Code(迭代版): 使用queue记录每一层。
/** * 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<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> ans; queue<TreeNode*> cur,next; if(root==NULL) return ans; cur.push(root); vector<int> levelval; while(!cur.empty()){ while(!cur.empty()){ if(cur.front()->left!=NULL) next.push(cur.front()->left); if(cur.front()->right!=NULL) next.push(cur.front()->right); levelval.push_back(cur.front()->val); cur.pop(); } swap(cur,next); ans.push_back(levelval); levelval.clear(); } return ans; }};
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]
Solution: 对前一题的结果进行翻转即可。
Code:
/** * 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<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> ans; getSons(root, 0, ans); reverse(ans.begin(), ans.end()); //翻转 return ans; }private: void getSons(TreeNode* root, int d, vector<vector<int>>& v){ if(root==NULL) return; if(d>=v.size()) v.push_back(vector<int>()); getSons(root->left, d+1, v); getSons(root->right, d+1, v); v[d].push_back(root->val); }};
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
Code:
/** * 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: bool isSymmetric(TreeNode* root) { if(root==NULL) return true; return isSym(root->left, root->right); }private: bool isSym(TreeNode* l, TreeNode* r){ if(l==NULL && r==NULL) return true; if(l!=NULL && r!=NULL){ if(l->val != r->val) return false; return isSym(l->left, r->right) && isSym(l->right, r->left); } return false; }};
- [Leetcode] 102, 107, 101
- Leetcode 107
- Leetcode 107
- LeetCode 107
- LEETCODE 101
- leetcode-101
- Leetcode 101
- Leetcode 101
- LeetCode-102,103,107,111总结
- LeetCode 2015.7.16 165,235,36,237,101,110,102,107,2
- LeetCode 102 N-Queens
- leetcode #102 in cpp
- Leetcode no. 102
- leetCode练习(102)
- LeetCode@102_Binary_Tree_Level_Order_Traversal
- [LeetCode] 102: Single Number
- Leetcode 102 & 106
- leetcode 107: Climbing Stairs
- Ecology7二次开发:自定义浏览按钮自定义页面设置返回值
- 1864
- Jam的计数法
- 代理模式
- 2017.8.17 总结
- [Leetcode] 102, 107, 101
- 51nod 1099 任务执行顺序 ACM
- 在Debian上安装完整的内核源码
- OpenCV编译遇到的一个常见错误-./bin: error while loading shared libraries: libopencv_highgui.so.3.2: cannot open
- margin负值问题
- 计算机视觉课程
- python
- 动态代理模式
- THINKPHP查询