110.
知道平衡二叉树的概念即可。
/** @lc app=leetcode.cn id=110 lang=cpp** [110] 平衡二叉树*/// @lc code=start
/*** 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) {}* };*/
#include<iostream>
using namespace std;
class Solution {
public:int getHeight(TreeNode* node){if(node==NULL)return 0;int left=getHeight(node->left);if(left==-1)return -1;int right=getHeight(node->right);if(right==-1)return -1;return abs(right-left)>1?-1:1+max(right,left);}bool isBalanced(TreeNode* root) {return getHeight(root)==-1?false:true;}
};
// @lc code=end
257.
/** @lc app=leetcode.cn id=257 lang=cpp** [257] 二叉树的所有路径*/// @lc code=start
/*** 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) {}* };*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:void travasal(TreeNode* cur,string path,vector<string>& result){path+=to_string(cur->val);if (cur->left == NULL && cur->right == NULL) {result.push_back(path);return;}if (cur->left) travasal(cur->left, path + "->", result); // 左if (cur->right) travasal(cur->right, path + "->", result); // 右}vector<string> binaryTreePaths(TreeNode* root) {vector<string> result;string path;if (root == NULL) return result;travasal(root, path, result);return result;}
};
// @lc code=end
404.
/** @lc app=leetcode.cn id=404 lang=cpp** [404] 左叶子之和*/// @lc code=start
/*** 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 sumOfLeftLeaves(TreeNode* root) {if (root == NULL) return 0;if (root->left == NULL && root->right== NULL) return 0;int leftValue = sumOfLeftLeaves(root->left); if (root->left && !root->left->left && !root->left->right) { // 左子树就是一个左叶子的情况leftValue = root->left->val;}int rightValue = sumOfLeftLeaves(root->right); int sum = leftValue + rightValue; return sum;}
};
// @lc code=end
222.
/** @lc app=leetcode.cn id=222 lang=cpp** [222] 完全二叉树的节点个数*/// @lc code=start
/*** 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 countNodes(TreeNode* root) {if (root == NULL) return 0;return 1 + countNodes(root->left) + countNodes(root->right);}
};
// @lc code=end