103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its zigzag level order traversal as:
[ [3], [20,9], [15,7] ]
Solution(1):全部从左到右读取,最后将需要从右到左读的翻转一下。
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>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
getSons(root, 0, ans);
for(int i=1; i<ans.size(); i+=2){
reverse( ans[i].begin(), ans[i].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);
}
};
Solution(2):在读取的时候就按照不同的方式插入。
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>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
getSons(root, 0, ans);
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);
if(d%2==0)
v[d].push_back(root->val);
else
v[d].insert(v[d].begin(), root->val);
}
};
94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it 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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
getSons(root, ans);
return ans;
}
private:
void getSons(TreeNode* root, vector<int>& v){
if(root==NULL) return;
getSons(root->left, v);
v.push_back(root->val);
getSons(root->right, v);
}
};
Solution(1): 使用栈,时间复杂度O(n),空间复杂度O(n)。
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<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> s; //不是用来记录父节点,而是记录待输出的节点
TreeNode* cur;
cur = root;
while(!s.empty() || cur!=NULL){
if(cur!=NULL){
s.push(cur);
cur = cur->left;
}else{
cur = s.top();
s.pop();
ans.push_back(cur->val);
cur = cur->right;
}
}
return ans;
}
};
Solution(2): Morris 中序遍历,使用线索二叉树(threaded binary tree),时间复杂度O(n),空间复杂度O(1)。
注:虽然大O复杂度和使用stack的方法相同,但是因为系数不同,这种做法的耗时比用stack的要长,是时间换空间的做法。
详细解释参考:http://blog.csdn.net/mxw976235955/article/details/39829973
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<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* cur = root;
while(cur!=NULL){
if(cur->left==NULL){
ans.push_back(cur->val);
cur = cur->right;
}else{
//找到当前节点的前驱节点
TreeNode* node = cur->left;
while(node->right!=NULL && node->right!=cur) node = node->right;
if(node->right==NULL){
//前驱节点连接到当前节点
node->right = cur;
cur = cur->left;
}else{
//该点之前已经访问过,再次访问说明其左子树已经遍历过了,断开前驱点连接
node->right = NULL;
ans.push_back(cur->val);
cur = cur->right;
}
}
}
return ans;
}
};
98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
2 / \ 1 3Binary tree
[2,1,3]
, return true.
Example 2:
1 / \ 2 3Binary tree
[1,2,3]
, return false.
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:
bool isValidBST(TreeNode* root) {
return isValidBST(root, LONG_MAX, LONG_MIN);//注意int到达边界的情况,所以判断最值时使用long
}
private:
bool isValidBST(TreeNode* root, long maxVal, long minVal){
if(root==NULL) return true;
if(root->val>=maxVal || root->val<=minVal) return false;
return isValidBST(root->left, root->val, minVal) && isValidBST(root->right, maxVal, root->val);
}
};