二叉树的层序遍历
解题思路:
这是对二叉树进行层级遍历考研,不同的是存储的方式,使用2维数组进行存储,所以我们先记录该层的数量,进行该层的vector存储,然后进行下一层的遍历,以此类推。
代码:
/**
* 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>> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int sz = q.size();
res.push_back(vector<int> ());
for(int i = 0; i < sz; i++) {
TreeNode* temp = q.front();
q.pop();
res.back().push_back(temp -> val);
if(temp -> left) q.push(temp -> left);
if(temp -> right) q.push(temp -> right);
}
}
return res;
}
};
1367. 二叉树中的列表
解题思路:
首先我们先使用枚举,列举出全部的判断情况
- head == NULL,说明链表全部都遍历完成,返回true
- root == NULL || root -> val != head -> val,说明树和链表不匹配,返回false
- 以上都不符合,说明该节点匹配成功,再进行root -> left和root -> right的遍历
然后我们找递归逻辑,对于根节点,我们进行dfs遍历判断(dfs(head , root)),如果不成功,就找根的左节点( isSubPath(head , root -> left) ),再不行就右节点( isSubPath(head , root -> right) )。再以根节点的左右节点为根节点来进行遍历,直到遍历完成。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* 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 isSubPath(ListNode* head, TreeNode* root) {
if(root == NULL) return false;
return dfs(head , root) || isSubPath(head , root -> left) || isSubPath(head , root -> right);
}
bool dfs(ListNode* head , TreeNode* root) {
if(head == NULL) return true;
if(root == NULL) return false;
if(root -> val != head -> val) return false;
return dfs(head -> next , root -> left) || dfs(head -> next , root -> right);
}
};
173. 二叉搜索树迭代器
解题思路:
这题中我们在构造函数中完成对根节点和全部的左节点都存储在栈中,为了起到从小到大从栈取出的顺序,在存储每个节点的右节点的全部左节点。判断是否存在下一个就相当于判断栈是否为空即可。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
stack<TreeNode*> s;
BSTIterator(TreeNode* root) {
while(root) {
s.push(root);
root = root -> left;
}
}
/** @return the next smallest number */
int next() {
TreeNode* temp = s.top();
s.pop();
int num = temp -> val;
temp = temp -> right;
while(temp) {
s.push(temp);
temp = temp -> left;
}
return num;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !s.empty();
}
};
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
路径总和 III
解题思路:
这题的重点在于如何求和,我们定义一个外部变量进行求和。所以我们在以pathSum函数求后面的值时,pathSum函数直接当作void函数使用,以避免重复求和的情况。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res = 0;
int pathSum(TreeNode* root, int sum) {
if(root == nullptr) return res;
dfs(root , sum , 0);
pathSum(root -> left , sum);
pathSum(root -> right , sum);
return res;
}
void dfs(TreeNode* root , int sum , int cur) {
if(root == nullptr) return;
cur += root -> val;
if(cur == sum) res++;
dfs(root -> left , sum , cur);
dfs(root -> right , sum , cur);
}
};
总结:
在dfs递归时,我们先求和再判断是否等于sum,当sum == cur时,也继续求值。
这题要掌握当使用外部变量求解。而且递归函数不是void函数时,要怎么返回值。
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
解题思路:
因为时二叉搜索树,所以可以通过数组来找公共祖先。当p 和 q都小于root时,证明公共祖先一定在root的左节点,同理都大于时证明在root的右节点,当处于root处于两者之间时,证明root就是公共祖先
代码:
/**
* 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p -> val < root -> val && q -> val < root -> val) return lowestCommonAncestor(root -> left , p , q);
if(p -> val > root -> val && q -> val > root -> val) return lowestCommonAncestor(root -> right , p , q);
return root;
}
};