完全二叉树的节点个数 leetcode 222
解法一:普通二叉树后序遍历
class Solution {
public:
int getNumber(TreeNode* node){
if(node==nullptr) return 0;
int leftNumber=getNumber(node->left);
int rightNumber=getNumber(node->right);
return leftNumber+rightNumber+1;
}
int countNodes(TreeNode* root) {
return getNumber(root);
}
};
解法二:完全二叉树
遍历根节点的左右子树,逐层向下直至找到满二叉树(从子树的左外侧和右外侧向下计算深度如果相同就是满二叉树,这个前提是这棵树是完全二叉树),由公式(2^k)-1计算满二叉树节点数量,然后返回上一层节点继续遍历。
class Solution {
public:
int getNumber(TreeNode* node){
if(node==nullptr) return 0;
TreeNode* left=node->left;
TreeNode* right=node->right;
int leftDepth=0,rightDepth=0;
while(left){
left=left->left;
leftDepth++;
}
while(right){
right=right->right;
rightDepth++;
}
if(rightDepth==leftDepth) return (2<<rightDepth)-1;
int leftNumber=getNumber(node->left);
int rightNumber=getNumber(node->right);
return leftNumber+rightNumber+1;
}
int countNodes(TreeNode* root) {
return getNumber(root);
}
};
2向左移n位,相当于2^(n+1)。
解法三:迭代层序遍历
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
queue<TreeNode*> que;
int res=0;
que.push(root);
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* tmp=que.front();
que.pop();
res++;
if(tmp->left) que.push(tmp->left);
if(tmp->right) que.push(tmp->right);
}
}
return res;
}
};
每pop一次res结果就加一来计算节点数。
平衡二叉树 leetcode 110
求高度用后序遍历,求深度用前序遍历.
高度就是从下往上看(比如本题判断是不是平衡二叉树,从下往上找有没有根节点左右子树高度大于1的),深度是从上往下看。
解法一:递归
class Solution {
public:
int getHeight(TreeNode* node){
if(node==nullptr) return 0;
int leftHeight=getHeight(node->left);
if(leftHeight==-1) return -1;
int rightHeight=getHeight(node->right);
if(rightHeight==-1) return -1;
if(abs(leftHeight-rightHeight)>1) return -1;
int res=(leftHeight>rightHeight?leftHeight:rightHeight)+1;
return res;
}
bool isBalanced(TreeNode* root) {
return getHeight(root)==-1?false:true;
}
};
解法二:迭代
class Solution {
public:
int getDepth(TreeNode* node){
stack<TreeNode*> st;
if(node!=nullptr) st.push(node);
int depth=0;
int result=0;
while(!st.empty()){
TreeNode* cur=st.top();
if(cur!=nullptr){
st.pop();
st.push(cur);
st.push(nullptr);
depth++;
if(cur->right) st.push(cur->right);
if(cur->left) st.push(cur->left);
}else{
st.pop();
st.pop();
depth--;
}
result=result>depth?result:depth;
}
return result;
}
bool isBalanced(TreeNode* root) {
stack<TreeNode*> st;
if(root==nullptr) return true;
st.push(root);
while(!st.empty()){
TreeNode* tmp=st.top();
st.pop();
if(abs(getDepth(tmp->left)-getDepth(tmp->right))>1) return false;
if(tmp->right) st.push(tmp->right);
if(tmp->left) st.push(tmp->left);
}
return true;
}
};
其中getDepth函数使用统一后序迭代,其中depth为当前深度,result为传入节点最大深度。
这个函数通过栈模拟的后序遍历找每一个节点的高度(其实是通过求传入节点为根节点的最大深度来求的高度)。
isBalanced函数中使用后序迭代。
二叉树的所有路径 leetcode 257
解法一:递归前序
class Solution {
public:
void traversal(TreeNode* node,vector<int>& path,vector<string>& res){
path.push_back(node->val);
if(node->left==nullptr&&node->right==nullptr){
string Spath;
for(int i=0;i<path.size()-1;i++){
Spath+=to_string(path[i]);
Spath+="->";
}
Spath+=to_string(path[path.size()-1]);
res.push_back(Spath);
return;
}
if(node->left){
traversal(node->left,path,res);
path.pop_back();
}
if(node->right){
traversal(node->right,path,res);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> res;
if(root==nullptr) return res;
traversal(root,path,res);
return res;
}
};
总结
1.回溯和递归是一一对应的,有一个递归,就要有一个回溯,回溯要和递归永远在一起。
2.to_string()函数将数值类型变量转化为字符串类型。
解法二:迭代前序
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> treeNode;
stack<string> path;
vector<string> res;
if(root==nullptr) return res;
treeNode.push(root); path.push(to_string(root->val));
while(!treeNode.empty()){
TreeNode* cur=treeNode.top();
treeNode.pop();
string Spath=path.top();
path.pop();
if(cur->left==nullptr&&cur->right==nullptr){
res.push_back(Spath);
}
if(cur->left) {
treeNode.push(cur->left);
path.push(Spath+"->"+to_string(cur->left->val));
}
if(cur->right) {
treeNode.push(cur->right);
path.push(Spath+"->"+to_string(cur->right->val));
}
}
return res;
}
};
左叶子之和 leetcode 404
解法一:递归后序
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
if(root->left==nullptr&&root->right==nullptr) return 0;
int leftNumber=sumOfLeftLeaves(root->left);
if(root->left!=nullptr&&root->left->left==nullptr&&root->left->right==nullptr){
leftNumber= root->left->val;
}
int rightNumber=sumOfLeftLeaves(root->right);
return leftNumber+rightNumber;
}
};
每层递归返回的是当前根节点的左右子树左叶子节点之和。所以才有第二个if语句判断递归结束。
解法二:迭代后序
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
stack<TreeNode*> st;
st.push(root);
int res=0;
while(!st.empty()){
TreeNode* cur=st.top();
st.pop();
if(cur->left&&!cur->left->left&&!cur->left->right){
res+=cur->left->val;
}
if(cur->right) st.push(cur->right);
if(cur->left) st.push(cur->left);
}
return res;
}
};