原题链接
/**
* 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> get_val(vector<TreeNode*> level)
{
vector<int> res;
for (auto &u : level)
res.push_back(u->val);
return res;
}
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
vector<vector<int>>res;
if (!root) return res;
vector<TreeNode*>level;
level.push_back(root);
res.push_back(get_val(level));
while (true)
{
vector<TreeNode*> newLevel;
for (auto &u : level)
{
if (u->left) newLevel.push_back(u->left);
if (u->right) newLevel.push_back(u->right);
}
if (newLevel.size())
{
res.push_back(get_val(newLevel));
level = newLevel;
}
else break;
}
return res;
}
};
原题链接
本题和上一道题差不多
就是需要定义一个变量
判断是否需要翻转
/**
* 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> get_val(vector<TreeNode*> level)
{
vector<int> res;
for (auto &u : level)
res.push_back(u->val);
return res;
}
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
vector<vector<int>>res;
if (!root) return res;
vector<TreeNode*>level;
level.push_back(root);
res.push_back(get_val(level));
bool zigzag = true;
while (true)
{
vector<TreeNode*> newLevel;
for (auto &u : level)
{
if (u->left) newLevel.push_back(u->left);
if (u->right) newLevel.push_back(u->right);
}
if (newLevel.size())
{
vector<int>temp = get_val(newLevel);
if (zigzag)
reverse(temp.begin(), temp.end());
res.push_back(temp);
level = newLevel;
}
else break;
zigzag = !zigzag;
}
return res;
}
};
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> printFromTopToBottom(TreeNode root) {
List<List<Integer>> ans = new ArrayList<>();
if(root == null){
return ans;
}
Queue<TreeNode> q1 = new LinkedList<TreeNode>();
q1.offer(root);
List<Integer> l = new ArrayList();
l.add(root.val);
ans.add(l);
boolean flag = false;
while(true) {
Queue<TreeNode> q2 = new LinkedList<TreeNode>();
for(TreeNode x : q1){
if(x.left != null) {
q2.offer(x.left);
}
if(x.right != null) {
q2.offer(x.right);
}
}
if(!q2.isEmpty()) {
List<Integer> l1 = new ArrayList();
for(TreeNode x : q2){
l1.add(x.val);
}
if(flag == false){
Collections.reverse(l1);
flag = true;
} else {
flag = false;
}
ans.add(l1);
q1 = q2;
} else {
break;
}
}
return ans;
}
}
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> printFromTopToBottom(TreeNode root) {
List<List<Integer>> ans = new ArrayList<>();
if(root == null){
return ans;
}
Queue<TreeNode> q1 = new LinkedList<TreeNode>();
q1.offer(root);
List<Integer> l = new ArrayList();
l.add(root.val);
ans.add(l);
while(true) {
Queue<TreeNode> q2 = new LinkedList<TreeNode>();
for(TreeNode x : q1){
if(x.left != null) {
q2.offer(x.left);
}
if(x.right != null) {
q2.offer(x.right);
}
}
if(!q2.isEmpty()) {
List<Integer> l1 = new ArrayList();
for(TreeNode x : q2){
l1.add(x.val);
}
ans.add(l1);
q1 = q2;
} else {
break;
}
}
return ans;
}
}
由于是后序遍历,所以最后一个结点就是根节点,又因为是二叉搜索树,所以从第一个结点开始所有比它小的结点就是它的左子树,其他就是它的右子树。如果右子树有点不大于根节点的话就说明不是一棵二叉搜索树,返回false。最后递归处理左右子树。
class Solution {
public:
vector<int> seq;//设成全局变量方便操作
bool verifySequenceOfBST(vector<int> sequence) {
seq = sequence;
return dfs(0, seq.size() - 1);
}
bool dfs(int l, int r)
{
//如果区间内啥也没有就说明把所有的结点都判断完了,却没有一个是有问题的,所以返回true
if (l >= r)
return true;
//取出根节点
int root = seq[r];
//找出所有从l开始连续的比根节点小的结点
int k = l;
while (k < r && seq[k] < root)
k ++;
//这时k就是右子树后序遍历中的第一个结点
//如果不满足二叉搜索树的性质就返回false
for (int i = k; i < r; i ++)
if (seq[i] < root)
return false;
//递归处理左右子树
//y总的视频里的代码是错的
//他写的是return dfs(l, k - 1) && dfs(k + 1, r);
//这样会WA
return dfs(l, k - 1) && dfs(k, r - 1);
}
};
class Solution {
public boolean verifySequenceOfBST(int [] sequence) {
seq = sequence;
return dfs(0, seq.length - 1);
}
private boolean dfs(int l, int r) {
if (l >= r) return true;
int root = seq[r];
int k = l;
while (k < r && seq[k] < root) k++;
for (int i = k; i < r; i++) {
if (seq[i] < root)
return false;
}
return dfs(l, k - 1) && dfs(k, r - 1);
}
private int[] seq;
}
本题就是一个dfs回溯问题
/**
* 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>> ans;
vector<vector<int>> findPath(TreeNode* root, int sum) {
vector<int> sup;
dfs(root,sum,sup);
return ans;
}
void dfs(TreeNode* root,int sum,vector<int>& sup)
{
if(root == NULL)
return;
sum -= root->val;
sup.push_back(root->val);
if(root->left == NULL && root->right == NULL && sum == 0)
ans.push_back(sup);
dfs(root->left,sum,sup);
dfs(root->right,sum,sup);
sum += root->val;
sup.pop_back();
}
};
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List> res = new ArrayList<>();
public List> findPath(TreeNode root, int sum) {
List list = new ArrayList<>();
dfs(root,list,sum,0);
return res;
}
void dfs(TreeNode root,List list,int sum,int ans){
if(root == null){
return;
}
list.add(root.val);
if(ans+root.val == sum && root.left == null && root.right == null){
res.add(new ArrayList<>(list));
}
dfs(root.left,list,sum,ans+root.val);
dfs(root.right,list,sum,ans+root.val);
list.remove(list.size() - 1);
}
}