110.平衡二叉树
class Solution { public: bool isBalanced(TreeNode* root) { // 使用层序遍历(不行,有bug) queue<TreeNode*> que; if(root == NULL) return true; que.push(root); bool start = false; int result = 0; while(!que.empty()) { int size = que.size(); for(int i = 0; i < size; i++) { TreeNode* tmp = que.front();printf("%d\n",tmp->val); que.pop(); if(tmp->left) que.push(tmp->left); else start = true; if(tmp->right) que.push(tmp->right); else start = true; } printf("--%d\n",result); if(start == true) result++; if(result > 2) { printf("res = %d",result); return false; } } return true; } };
class Solution { public: int getHeight(TreeNode* root) { if(root == NULL) return 0; int left = getHeight(root->left); if(left == -1) return -1; int right = getHeight(root->right); if(right == -1) return -1; printf("%d %d %d\n", max(left,right),left,right); if(abs(left-right) > 1) { return -1; } return max(left,right) + 1; } bool isBalanced(TreeNode* root) { // 使用递归后序遍历 if(getHeight(root) == -1) return false; return true; } };
257.二叉树的所有路径
题目链接:力扣
class Solution { public: void traversal(TreeNode* node,vector<int>& vec, vector<string>& result) { if(node == NULL) { return; } vec.push_back(node->val); if(node->left) { traversal(node->left,vec,result); vec.pop_back(); } if(node->right) { traversal(node->right,vec,result); vec.pop_back(); } if(node->left == NULL && node->right == NULL) { string str = to_string(vec[0]); for(int i = 1; i < vec.size(); i++) { str += "->"; str += to_string(vec[i]); } result.push_back(str); } } vector<string> binaryTreePaths(TreeNode* root) { // 递归前序遍历 vector<int> vec; vector<string> result; traversal(root,vec,result); return result; } };
class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { // 迭代法 vector<string> result; // 存放最后结果 stack<string> path; // 存放当前节点路径 if(root == NULL) return result; stack<TreeNode*> st; st.push(root); path.push(to_string(root->val)); while(!st.empty()) { TreeNode* cur = st.top(); st.pop(); string str = path.top(); path.pop(); if(cur->left == NULL && cur->right == NULL) { result.push_back(str); } if(cur->right) { st.push(cur->right); path.push(str + "->" + to_string(cur->right->val)); } if(cur->left) { st.push(cur->left); path.push(str + "->" + to_string(cur->left->val)); } } return result; } };
404.左叶子之和
题目链接:力扣
class Solution { public: int traversal(TreeNode* node) { if(node == NULL) return 0; if(node->left == NULL && node->right == NULL) return 0; int leftNum = 0; if(node->left != NULL && node->left->left == NULL && node->left->right == NULL) { leftNum = node->left->val; } return leftNum + traversal(node->left) + traversal(node->right); } int sumOfLeftLeaves(TreeNode* root) { // 递归前序遍历 return traversal(root); } };
class Solution { public: int sumOfLeftLeaves(TreeNode* root) { // 迭代法,前序 int result = 0; if(root == NULL) return result; stack<TreeNode*> st; st.push(root); while(!st.empty()) { TreeNode* cur = st.top(); st.pop(); if(cur->left && cur->left->left == NULL && cur->left->right == NULL) { result += cur->left->val; } if(cur->right) { st.push(cur->right); } if(cur->left) { st.push(cur->left); } } return result; } };