94. 二叉树的中序遍历
/**
* 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:
void inorderHelper(TreeNode* root, vector<int>& res) {
if (root == nullptr) return;
inorderHelper(root->left, res); // 遍历左子树
res.push_back(root->val); // 记录当前节点值
inorderHelper(root->right, res); // 遍历右子树
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
inorderHelper(root, res); // 调用辅助函数填充 res
return res;
}
};
102. 二叉树层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector <vector <int>> ret;
if (!root) {
return ret;
}
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
ret.push_back(vector <int> ());
for (int i = 1; i <= currentLevelSize; ++i) {
auto node = q.front(); q.pop();
ret.back().push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
};
226.翻转二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null; //空
TreeNode temp = root.left; //临时变量
root.left = root.right; //左子树赋值为原来的右子树
root.right = temp; //右子树赋值为原来的左子树
invertTree(root.left); //递归左子树
invertTree(root.right); //递归右子树
return root;
}
}
101.对称二叉树
给你一个二叉树的根节点 root , 检查它是否轴对称
/**
* 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:
bool check(TreeNode *p,TreeNode *q)
{
if(!p && !q) //空树
return true;
if(!p || !q) //只有左结点或者只有右结点
return false;
return p->val == q->val && check(p->left,q->right) && check(p->right,q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root->left,root->right);
}
};
543.二叉树的直径
/**
* 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 maxDepth(TreeNode* root, int& ans)
{
if (!root)return 0;
int left = maxDepth(root->left, ans);
int right = maxDepth(root->right, ans);
ans = max(ans, left + right);
return max(left, right) + 1;
}
int diameterOfBinaryTree(TreeNode* root) {
int ans = 0;
maxDepth(root, ans);
return ans;
}
};
108.将有序数组转换为二叉树
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 平衡 二叉搜索树。
思路:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function(nums) {
function dfs(left,right)
{
if(left===right)
return null;
const m = Math.floor((left+right)/2);
return new TreeNode(nums[m],dfs(left,m),dfs(m+1,right));
}
return dfs(0,nums.length);
};
98.验证二叉搜索树
思路:二叉树的中序遍历
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
let pre=-Infinity;
function dfs(p)
{
if(p==null)
return true;
if(!dfs(p.left)||p.val<=pre)
return false;
pre=p.val;
return dfs(p.right);
}
return dfs(root);
};
230.二叉搜索树中第k小的元素
给定一个二叉搜索树的根节点 root ,和一个整数 k ,请你设计一个算法查找其中第 k 小的元素(从 1 开始计数)。 思路: 由于中序遍历就是在从小到大遍历节点值,所以遍历到的第 k 个节点值就是答案。 在中序遍历,即「左-根-右」的过程中,每次递归完左子树,就把 k 减少 1,表示我们按照中序遍历访问到了一个节点。如果减一后 k 变成 0,那么答案就是当前节点的值,用一个外部变量 ans 记录。
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function(root, k) {
let ans=0;
function dfs(p)
{
if(p==null)
return;
dfs(p.left);
if(--k === 0)
ans=p.val;
dfs(p.right);
}
dfs(root);
return ans;
};
时间复杂度:O(n),其中 n 是二叉树的大小(节点个数)。 空间复杂度:O(h),其中 h 是树高,递归需要 O(h) 的栈空间。最坏情况下树是一条链,h=n,空间复杂度为 O(n)。
199.二叉树的右视图
思路:先递归右子树,再递归左子树,当某个深度首次到达时,对应的节点就在右视图中。
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var rightSideView = function(root) {
const ans=[]; //临时数组,用于判断每层深度是否首次遇到
function dfs(p,depth)
{
if(p === null) //空树
return;
if(depth==ans.length) //该层深度是否首次遇到,首次遇到就是右试图结点
ans.push(p.val); //将该节点推入数组中
dfs(p.right,depth+1); //先递归遍历右子树
dfs(p.left,depth+1);
}
dfs(root,0);
return ans;
};
时间复杂度:O(n),其中 n 是二叉树的节点个数。 空间复杂度:O(h),其中 h 是二叉树的高度。递归需要 O(h) 的栈空间。最坏情况下,二叉树退化成一条链,递归需要 O(n) 的栈空间。
114.二叉树展开为链表
思路:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var flatten = function(root) {
let head=null;
function dfs(p)
{
if(p===null)
return;
dfs(p.right);
dfs(p.left);
p.left=null;
p.right=head;
head=p;
}
dfs(root);
};