目录
- 111.二叉树的最小深度
- 671.二叉树中第二小的节点
- 687.最长同值路径
- 112. 路径总和
- 101. 对称二叉树
- 110. 平衡二叉树
- 783. 二叉搜索树节点最小距离
- 404. 左叶子之和
- 面试题 04.04. 检查平衡性
- 530. 二叉搜索树的最小绝对差
- 剑指 Offer 28. 对称的二叉树
- 100. 相同的树
- 257. 二叉树的所有路径
- 235. 二叉搜索树的最近公共祖先
- 剑指 Offer 32 - II. 从上到下打印二叉树 II
- 面试题68 - II. 二叉树的最近公共祖先
- 104. 二叉树的最大深度
- 226. 翻转二叉树
- 938. 二叉搜索树的范围和
- 剑指 Offer 27. 二叉树的镜像
- 剑指 Offer 34. 二叉树中和为某一值的路径
- 102. 二叉树的层序遍历
- 剑指 Offer 32 - I. 从上到下打印二叉树
- 144. 二叉树的前序遍历
- 94. 二叉树的中序遍历
111.二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最小深度 2.
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)return 0;
if(root&&!root->left&&!root->right)return 1;
if(root){
int left_depth=minDepth(root->left);
int right_depth=minDepth(root->right);
cout<<left_depth<<" "<<right_depth<<endl;
if(left_depth==0&&right_depth!=0)return right_depth+1;
else if(left_depth!=0&&right_depth==0)return left_depth+1;
else return min(left_depth,right_depth)+1;
}
return 0;
}
};
671.二叉树中第二小的节点
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。
示例 1:
输入:
2
/
2 5
/
5 7
输出: 5
说明: 最小的值是 2 ,第二小的值是 5 。
示例 2:
输入:
2
/
2 2
输出: -1
说明: 最小的值是 2, 但是不存在第二小的值。
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
vector<int>layer_value;
if(!root)return -1;
if(root&&!root->left&&!root->right)return -1;
queue<TreeNode*>queueNode;
queueNode.push(root);
while(!queueNode.empty()){
int n=queueNode.size();
for(int i=0;i<n;++i){
TreeNode* pNode=queueNode.front();
queueNode.pop();
layer_value.push_back(pNode->val);
if(pNode->left)queueNode.push(pNode->left);
if(pNode->right)queueNode.push(pNode->right);
}
}
sort(layer_value.begin(),layer_value.end());
for(int i=1;i<layer_value.size();++i){
if(layer_value[i]>layer_value[0])return layer_value[i];
}
return -1;
}
};
687.最长同值路径
给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值。 这条路径可以经过也可以不经过根节点。
注意:两个节点之间的路径长度由它们之间的边数表示。
示例 1:
输入:
5
/ \
4 5
/ \ \
1 1 5
输出:
2
示例 2:
输入:
1
/ \
4 5
/ \ \
4 4 5
输出:
2
注意: 给定的二叉树不超过10000个结点。 树的高度不超过1000。
class Solution {
public:
int dfs(TreeNode* root,int& res)
{
if(!root)
return 0;
int l=0,r=0;
if(root->left)
{
int tmp = dfs(root->left,res);
if(root->val == root->left->val)
l = tmp+1;
}
if(root->right)
{
int tmp = dfs(root->right,res);
if(root->val == root->right->val)
r = tmp+1;
}
res = res>(r+l)?res:r+l;
return l>r?l:r;
}
int longestUnivaluePath(TreeNode* root) {
int res = 0;
dfs(root,res);
return res;
}
};
112. 路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22,
5
/
4 8
/ /
11 13 4
/ \
7 2 1
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
class Solution
{
public:
bool hasPathSum(TreeNode* root, int sum)
{
binaryPaths(root, 0,sum);
return record;
}
private:
bool record = false;
void binaryPaths(TreeNode* root, int obj_sum,int sum)
{
if (!root) { return; }
obj_sum += root->val;
if (!root->left && !root->right)
{
if (obj_sum == sum) { record = true; }
else { return; }
}
binaryPaths(root->left, obj_sum, sum);
binaryPaths(root->right, obj_sum, sum);
}
};
101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
进阶:
你可以运用递归和迭代两种方法解决这个问题吗?
class Solution {
public:
bool isSymmetric(TreeNode* root)
{
if (!root) { return true; }
if (root->left == NULL && root->right == NULL && root != NULL) { return true; }
queue<TreeNode*> node_queue1;
queue<TreeNode*> node_queue2;
node_queue1.push(root->left);
node_queue2.push(root->right);
while (!node_queue1.empty() && !node_queue2.empty())
{
TreeNode* node1 = node_queue1.front();
node_queue1.pop();
TreeNode* node2 = node_queue2.front();
node_queue2.pop();
if (!node1 && !node2) { continue; }
if ((!node1 && node2) || (node1 && !node2) || (node1->val != node2->val)) { return false; }
node_queue1.push(node1->left);
node_queue1.push(node1->right);
node_queue2.push(node2->right);
node_queue2.push(node2->left);
}
return true;
}
};
110. 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3
/
9 20
/
15 7
返回 true 。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/
2 2
/
3 3
/
4 4
返回 false 。
class Solution
{
public:
int maxDepth(TreeNode* root)
{
if (root == NULL) { return 0; }
else
{
int left_height = maxDepth(root->left);
int right_height = maxDepth(root->right);
return max(left_height, right_height) + 1;
}
}
bool isBalanced(TreeNode* root)
{
if (!root) { return true; }
if (root->left == NULL && root->right == NULL && root != NULL) { return true; }
queue<TreeNode*> node_queue;
node_queue.push(root);
while(!node_queue.empty())
{
TreeNode* node = node_queue.front();
node_queue.pop();
int left_depth = maxDepth(node->left);
int right_depth = maxDepth(node->right);
if (abs(left_depth - right_depth) > 1) { return false; }
if (node->left != NULL) { node_queue.push(node->left); }
if (node->right != NULL) { node_queue.push(node->right); }
}
return true;
}
};
783. 二叉搜索树节点最小距离
给定一个二叉搜索树的根节点 root,返回树中任意两节点的差的最小值。
示例:
输入: root = [4,2,6,1,3,null,null]
输出: 1
解释:
注意,root是树节点对象(TreeNode object),而不是数组。
给定的树 [4,2,6,1,3,null,null] 可表示为下图:
4
/
2 6
/ \
1 3
最小的差值是 1, 它是节点1和节点2的差值, 也是节点3和节点2的差值。
注意:
二叉树的大小范围在 2 到 100。
二叉树总是有效的,每个节点的值都是整数,且不重复。
本题与 530:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ 相同
class Solution {
public:
int min_num = int(1e9);
vector<int>result;
void InorderTravelsal(TreeNode* root)
{
if (!root) { return; }
InorderTravelsal(root->left);
result.push_back(root->val);
InorderTravelsal(root->right);
}
int minDiffInBST(TreeNode* root)
{
InorderTravelsal(root);
for (int i = 0; i < result.size(); ++i)
{
if (i == result.size() - 1) { continue; }
else{
if (abs(result[i + 1] - result[i]) < min_num) {
min_num = result[i + 1] - result[i];
}
}
}
return min_num;
}
};
404. 左叶子之和
计算给定二叉树的所有左叶子之和。
示例:
3
/
9 20
/
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
class Solution
{
public:
Solution() { sum = 0; }
int sumOfLeftLeaves(TreeNode* root)
{
if (!root) { return 0; }
if (root != NULL && root->left == NULL && root->right == NULL) { return 0; }
if (root->left && (!root->left->left && !root->left->right))
{
sum += root->left->val;
}
sumOfLeftLeaves(root->left);
sumOfLeftLeaves(root->right);
return sum;
}
private:
int sum;
};
面试题 04.04. 检查平衡性
实现一个函数,检查二叉树是否平衡。在这个问题中,平衡树的定义如下:任意一个节点,其两棵子树的高度差不超过 1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3
/
9 20
/
15 7
返回 true 。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/
2 2
/
3 3
/
4 4
返回 false 。
class Solution
{
public:
int maxDepth(TreeNode* root)
{
if (!root) { return 0; }
int left_maxDepth = maxDepth(root->left);
int right_maxDepth = maxDepth(root->right);
return max(left_maxDepth, right_maxDepth) + 1;
}
bool isBalanced(TreeNode* root)
{
if (!root) { return true; }
if (root && !root->left && !root->right) { return true; }
queue<TreeNode*> node_queue;
node_queue.push(root);
while (!node_queue.empty())
{
TreeNode* node = node_queue.front();
node_queue.pop();
int node_left_maxDepth = maxDepth(node->left);
int node_right_maxDepth = maxDepth(node->right);
if (abs(node_left_maxDepth - node_right_maxDepth) > 1) { return false; }
else
{
if (node->left) { node_queue.push(node->left); }
if (node->right) { node_queue.push(node->right); }
}
}
return true;
}
};
530. 二叉搜索树的最小绝对差
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
示例:
输入:
1
3
/
2
输出:
1
解释:
最小绝对差为 1,其中 2 和 1 的差的绝对值为 1(或者 2 和 3)。
提示:
树中至少有 2 个节点。
本题与 783 https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/ 相同
class Solution {
public:
int min_num = int(1e9);
vector<int>result;
void InorderTravelsal(TreeNode* root)
{
if (!root) { return; }
InorderTravelsal(root->left);
result.push_back(root->val);
InorderTravelsal(root->right);
}
int getMinimumDifference(TreeNode* root)
{
InorderTravelsal(root);
for (int i = 0; i < result.size(); ++i)
{
if (i == result.size() - 1) { continue; }
else{
if (abs(result[i + 1] - result[i]) < min_num) {
min_num = result[i + 1] - result[i];
}
}
}
return min_num;
}
};
剑指 Offer 28. 对称的二叉树
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
限制:
0 <= 节点个数 <= 1000
注意:本题与主站 101 题相同:https://leetcode-cn.com/problems/symmetric-tree/
class Solution {
public:
bool isSymmetric(TreeNode* root)
{
if (!root) { return true; }
if (root->left == NULL && root->right == NULL && root != NULL) { return true; }
queue<TreeNode*> node_queue1;
queue<TreeNode*> node_queue2;
node_queue1.push(root->left);
node_queue2.push(root->right);
while (!node_queue1.empty() && !node_queue2.empty())
{
TreeNode* node1 = node_queue1.front();
node_queue1.pop();
TreeNode* node2 = node_queue2.front();
node_queue2.pop();
if (!node1 && !node2) { continue; }
if ((!node1 && node2) || (node1 && !node2) || (node1->val != node2->val)) { return false; }
node_queue1.push(node1->left);
node_queue1.push(node1->right);
node_queue2.push(node2->right);
node_queue2.push(node2->left);
}
return true;
}
};
100. 相同的树
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入: 1 1
/ \ /
2 3 2 3
[1,2,3], [1,2,3]
输出: true
示例 2:
输入: 1 1
/
2 2
[1,2], [1,null,2]
输出: false
示例 3:
输入: 1 1
/ \ /
2 1 1 2
[1,2,1], [1,1,2]
输出: false
class Solution
{
public:
bool isSameTree(TreeNode* p, TreeNode* q)
{
if (!p && !q) { return true; }
if ((!p && q) || (!q && p)) { return false; }
if (p->val != q->val) { return false; }
return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}
};
257. 二叉树的所有路径
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入:
1
/
2 3
5
输出: [“1->2->5”, “1->3”]
解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
class Solution
{
public:
vector<string> binaryTreePaths(TreeNode* root)
{
binaryTreePath(root, "", true);
return result;
}
private:
vector<string> result;
void binaryTreePath(TreeNode* root, string str, bool isRoot)
{
if (!root) { return; }
str += (isRoot ? "" : "->") + to_string(root->val);
if (!root->left && !root->right)
{
result.push_back(str);
return;
}
binaryTreePath(root->left, str, false);
binaryTreePath(root->right, str, false);
}
};
235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root)return NULL;
if(root->val<p->val&&root->val<q->val){
return lowestCommonAncestor(root->right,p,q);
}
if(root->val>p->val&&root->val>q->val){
return lowestCommonAncestor(root->left,p,q);
}
return root;
}
};
剑指 Offer 32 - II. 从上到下打印二叉树 II
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
提示:
节点总数 <= 1000
注意:本题与主站 102 题相同:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
class Solution
{
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>> result;
vector<int> layer_value;
if (!root) { return result; }
if (root && root->left == NULL && root->right == NULL)
{
layer_value.push_back(root->val);
result.push_back(layer_value);
return result;
}
queue<TreeNode*> node_queue;
node_queue.push(root);
while (!node_queue.empty())
{
int n = node_queue.size();
for (int i = 0;i < n;i++)
{
TreeNode* node = node_queue.front();
node_queue.pop();
layer_value.push_back(node->val);
if (node->left) { node_queue.push(node->left); }
if (node->right) { node_queue.push(node->right); }
}
result.push_back(layer_value);
for (int i = 0; i < n; i++) { layer_value.pop_back(); }
}
return result;
}
};
面试题68 - II. 二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]
示例 1:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
示例 2:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出: 5
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉树中。
注意:本题与主站 236 题相同:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root)return NULL;
if(root==p||q==root)return root;
TreeNode* left=lowestCommonAncestor(root->left,p,q);
TreeNode* right=lowestCommonAncestor(root->right,p,q);
if(left&&right)return root;
if(left&&!right)return left;
if(right&&!left)return right;
return NULL;
}
};
104. 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
class Solution {
public:
int maxDepth(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
int left_height = maxDepth(root->left);
int right_height = maxDepth(root->right);
return max(left_height, right_height) + 1;
}
}
};
226. 翻转二叉树
翻转一棵二叉树。
示例:
输入:
4
/
2 7
/ \ /
1 3 6 9
输出:
4
/
7 2
/ \ /
9 6 3 1
class Solution
{
public:
TreeNode* invertTree(TreeNode* root)
{
TreeNode* tree_temp = root;
if (!tree_temp || (tree_temp && !tree_temp->left && !tree_temp->right)) { return tree_temp; }
if (tree_temp)
{
TreeNode* temp = tree_temp->left;
tree_temp->left = tree_temp->right;
root->right = temp;
invertTree(tree_temp->left);
invertTree(tree_temp->right);
}
return tree_temp;
}
};
938. 二叉搜索树的范围和
给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和。
二叉搜索树保证具有唯一的值。
示例 1:
输入:root = [10,5,15,3,7,null,18], L = 7, R = 15
输出:32
示例 2:
输入:root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
输出:23
提示:
树中的结点数量最多为 10000 个。
最终的答案保证小于 2^31。
class Solution {
public:
int rangeSumBST(TreeNode* root, int L, int R) {
int sum = 0;
int last = -1;
DFS(root, sum, L, R);
return sum;
}
void DFS(TreeNode* root, int& sum,const int L, const int R){
if(root==nullptr) return;
//左子树
if(root->left) DFS(root->left,sum,L,R);
//如果当前结点的值在L-R之间,就相加
if(root->val>=L && root->val<=R)sum += root->val;
//右子树
if(root->right) DFS(root->right,sum,L,R);
}
};
剑指 Offer 27. 二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
4
/
2 7
/ \ /
1 3 6 9
镜像输出:
4
/
7 2
/ \ /
9 6 3 1
示例 1:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
class Solution
{
public:
TreeNode* mirrorTree(TreeNode* root)
{
TreeNode* tree_tmp;
tree_tmp = root;
if (!tree_tmp) { return tree_tmp; }
if (tree_tmp != NULL && tree_tmp->left == NULL && tree_tmp->right == NULL) { return tree_tmp; }
if (tree_tmp)
{
TreeNode* temp = tree_tmp->left;
tree_tmp->left = tree_tmp->right;
tree_tmp->right = temp;
mirrorTree(tree_tmp->left);
mirrorTree(tree_tmp->right);
}
return tree_tmp;
}
};
剑指 Offer 34. 二叉树中和为某一值的路径
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
示例:
给定如下二叉树,以及目标和 sum = 22,
5
/
4 8
/ /
11 13 4
/ \ /
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
提示:
节点总数 <= 10000
注意:本题与主站 113 题相同:https://leetcode-cn.com/problems/path-sum-ii/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(!root)return {};
vector<vector<int> >ans;
vector<int>path;
int currenSum=0;
Findpath(root,ans,path,sum,currenSum);
return ans;
}
void Findpath(TreeNode* root,vector<vector<int>>&ans,vector<int>&path,int sum,int& currenSum){
currenSum+=root->val;
path.push_back(root->val);
bool isLeaf=!root->left&&!root->right;
if(currenSum==sum&&isLeaf) ans.push_back(path);
if(root->left)Findpath(root->left,ans,path,sum,currenSum);
if(root->right)Findpath(root->right,ans,path,sum,currenSum);
currenSum-=root->val;
path.pop_back();
}
};
102. 二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root)return {};
if(root&&!root->left&&!root->right)return {{root->val}};
vector<vector<int> >ans;
vector<int>layer_value;
queue<TreeNode*>queueNode;
queueNode.push(root);
while(!queueNode.empty()){
int n=queueNode.size();
for(int i=0;i<n;++i){
TreeNode* pNode=queueNode.front();
queueNode.pop();
layer_value.push_back(pNode->val);\
if(pNode->left)queueNode.push(pNode->left);
if(pNode->right)queueNode.push(pNode->right);
}
ans.push_back(layer_value);
for(auto x:layer_value)layer_value.pop_back();
}
return ans;
}
};
剑指 Offer 32 - I. 从上到下打印二叉树
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回:
[3,9,20,15,7]
class Solution {
public:
vector<int> levelOrder(TreeNode* root) {
if(!root)return {};
vector<int>ans;
std::deque<TreeNode*>dequeTreeNode;
dequeTreeNode.push_back(root);
ans.push_back(root->val);
while(dequeTreeNode.size()){
TreeNode* pNode=dequeTreeNode.front();
dequeTreeNode.pop_front();
if(pNode->left){
dequeTreeNode.push_back(pNode->left);
ans.push_back(pNode->left->val);
}
if(pNode->right){
dequeTreeNode.push_back(pNode->right);
ans.push_back(pNode->right->val);
}
}
return ans;
}
};
144. 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
class Solution {
private:
vector<int>result;
public:
vector<int> preorderTraversal(TreeNode* root) {
if(root){
result.push_back(root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
return result;
}
};
94. 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
class Solution {
private:
vector<int>ans;
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left);
ans.push_back(root->val);
inorderTraversal(root->right);
}
return ans;
}
};