LeetCode二叉树题总结(持续更新)
Posted karshey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode二叉树题总结(持续更新)相关的知识,希望对你有一定的参考价值。
文章目录
理论
144. 二叉树的前序遍历(递归与迭代)
中左右:
/**
* 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:
vector<int> preorderTraversal(TreeNode* root)
vector<int>ans;
traversal(root,ans);
return ans;
void traversal(TreeNode *root,vector<int>&ans)
if(root==NULL) return;
ans.push_back(root->val);
traversal(root->left,ans);
traversal(root->right,ans);
;
栈:中右左。
动图见这里
class Solution
public:
vector<int> preorderTraversal(TreeNode* root)
vector<int>ans;
if(!root) return ans;
stack<TreeNode*>st;
st.push(root);
while(st.size())
TreeNode* node=st.top();
st.pop();
ans.push_back(node->val);
if(node->right) st.push(node->right);
if(node->left) st.push(node->left);
return ans;
;
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:
vector<int> inorderTraversal(TreeNode* root)
vector<int>ans;
traversal(root,ans);
return ans;
void traversal(TreeNode *root,vector<int>&ans)
if(root==NULL) return;
traversal(root->left,ans);
ans.push_back(root->val);
traversal(root->right,ans);
;
class Solution
public:
vector<int> inorderTraversal(TreeNode* root)
vector<int>ans;
stack<TreeNode*>st;
if(!root) return ans;
TreeNode* now=root;
while(now||st.size())
if(now)
st.push(now); //左
now=now->left;
else
ans.push_back(st.top()->val); //中
now=st.top()->right; //右
st.pop();
return ans;
;
145. 二叉树的后序遍历(递归与迭代)
class Solution
public:
vector<int> postorderTraversal(TreeNode* root)
vector<int>ans;
traversal(root,ans);
return ans;
void traversal(TreeNode *root,vector<int>&ans)
if(root==NULL) return;
traversal(root->left,ans);
traversal(root->right,ans);
ans.push_back(root->val);
;
class Solution
public:
vector<int> postorderTraversal(TreeNode* root)
vector<int>ans;
stack<TreeNode*>st;
if(!root) return ans;
st.push(root);
while(st.size())
TreeNode* temp=st.top();
ans.push_back(temp->val);
st.pop();
if(temp->left) st.push(temp->left);
if(temp->right) st.push(temp->right);
reverse(ans.begin(),ans.end());
return ans;
;
102. 二叉树的层序遍历
类似BFS的模板了。
class Solution
public:
vector<vector<int>> levelOrder(TreeNode* root)
vector<vector<int>>ans;
if(!root) return ans;
queue<TreeNode*>q;
q.push(root);
while(q.size())
vector<int>temp;
int s=q.size();
for(int i=0;i<s;i++)
TreeNode* now=q.front();
q.pop();
temp.push_back(now->val);
if(now->left) q.push(now->left);
if(now->right) q.push(now->right);
ans.push_back(temp);
return ans;
;
226. 翻转二叉树
感觉层序遍历最直观。
class Solution
public:
TreeNode* invertTree(TreeNode* root)
queue<TreeNode*>q;
if(!root) return NULL;
TreeNode* now;
q.push(root);
while(q.size())
int s=q.size();
for(int i=0;i<s;i++)
now=q.front();
q.pop();
swap(now->left,now->right);
if(now->left) q.push(now->left);
if(now->right) q.push(now->right);
return root;
;
101. 对称二叉树
class Solution
public:
bool isSymmetric(TreeNode* root)
if(!root) return true;
TreeNode* le;
TreeNode* ri;
queue<TreeNode*>q;
q.push(root->left);
q.push(root->right);
while(q.size())
le=q.front();q.pop();
ri=q.front();q.pop();
//都空
if(!le&&!ri) continue;
//都不空且相等
else if(le&&ri&&le->val==ri->val)
q.push(le->left);
q.push(ri->right);
q.push(le->right);
q.push(ri->left);
else return false;
return true;
;
222. 完全二叉树的节点个数(利用完全二叉树性质)
class Solution
public:
int countNodes(TreeNode* root)
if(!root) return 0;
TreeNode* le=root->left,*ri=root->right;
int leNum=0,riNum=0;
while(le)
leNum++;
le=le->left;
while(ri)
riNum++;
ri=ri->right;
//满二叉树
if(leNum==riNum)
return (2<<leNum)-1;
return countNodes(root->left)+countNodes(root->right)+1;
;
110. 平衡二叉树(递归)
class Solution
public:
bool isBalanced(TreeNode* root)
if(getHeight(root)!=-1) return true;
else return false;
//二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。
int getHeight(TreeNode *root)
if(!root) return 0;
int le=getHeight(root->left);
int ri=getHeight(root->right);
if(le==-1||ri==-1) return -1;
else return abs(le-ri)>1?-1:max(le,ri)+1;
;
100. 相同的树(递归)
class Solution
public:
bool isSameTree(TreeNode* p, TreeNode* q)
return check(p,q);
bool check(TreeNode* t1,TreeNode* t2)
if(!t1&&!t2) return true;
else if(t1&&!t2) return false;
else if(!t1&&t2) return false;
else if(t1&&t2&&t1->val!=t2->val) return false;
bool ans=check(t1->left,t2->left)*check(t1->right,t2->right);
return ans;
;
257. 二叉树的所有路径(经典dfs)
class Solution
public:
vector<string>res;
vector<string> binaryTreePaths(TreeNode* root)
res.clear();
if(!root) return res;
vector<int>ans;
ans.push_back(root->val);
dfs(root,ans);
return res;
void dfs(TreeNode* node,vector<int>&ans)
//到底了
if(!node->left&&!node->right)
res.push_back(IntToString(ans));
else
if(node->left)
ans.push_back(node->left->val);
dfs(node->left,ans);
ans.pop_back();
if(node->right)
ans.push_back(node->right->val);
dfs(node->right,ans);
ans.pop_back();
string IntToString(vector<int>ans)
string anss;
for(int i=0;i<ans.size();i++)
anss+=to_string(ans[i]);
if(i!=ans.size()-1) anss+="->";
return anss;
;
113. 路径总和 II(经典dfs)
做这道题的时候有一种在打天梯赛的感觉。
class Solution
public:
vector<vector<int>>ans;
int targetSum1;
vector<vector<int>> pathSum(TreeNode* root, int targetSum)
if(!root) return ans;
targetSum1=targetSum;
vector<int>anss;
anss.push_back(root->val);
dfs(root,anss,root->val);
return ans;
void dfs(TreeNode* now,vector<int>&anss,int sum)
//叶子节点
if(!now->left&&!now->right)
if(sum==targetSum1)
ans.push_back(anss);
else
if(now->left)
sum+=now->left->val;
anss.push_back(now->left->val);
dfs(now->left,anss,sum);
sum-=now->left->val;
anss.pop_back();
if(now->right)
sum+=now->right->val;
anss.push_back(now->right->val);
dfs(以上是关于LeetCode二叉树题总结(持续更新)的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode与《代码随想录》二叉树篇:做题笔记与总结-JavaScript版
Leetcode 102: 按层遍历二叉树, 112 路径总和问题
Leetcode 102: 按层遍历二叉树, 112 路径总和问题