文章目录
- 110平衡二叉树
- c++ 代码实现
- python 代码实现
- 257.二叉树的所有路径
- c++代码实现
- python 代码实现
- 404左叶子之和
- c++ 代码实现
- python 代码
110平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:true
示例 2:
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例 3:
输入:root = []
输出:true
提示:
- 树中的节点数在范围
[0, 5000]
内 -104 <= Node.val <= 104
c++ 代码实现
/**
* 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:
// 递归三要素
// 1.函数参数
int getHeight(TreeNode *node) {
// 停止条件
if (node == nullptr) return 0;
int leftH = getHeight(node->left);
// 是否已超过 1
if (leftH == -1) return -1;
int rightH = getHeight(node->right);
// 是否已超过 1
if (rightH == -1) return -1;
// 高度差是否超过 1 。大于返回 -1
if (abs(leftH - rightH) > 1) {
return -1;
}else{
// 计算当前最大
return 1 + max(leftH, rightH);
}
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
python 代码实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def getHeight(self, root):
if not root:
return 0
lh = self.getHeight(root.left)
if lh == -1:
return -1
rh = self.getHeight(root.right)
if rh == -1:
return -1
if abs(lh - rh) > 1:
return -1
else:
return (1 + max(lh, rh))
def isBalanced(self, root: Optional[TreeNode]) -> bool:
if self.getHeight(root) == -1:
return False
else:
return True
257.二叉树的所有路径
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5]
输出:["1->2->5","1->3"]
示例 2:
输入:root = [1]
输出:["1"]
提示:
- 树中节点的数目在范围
[1, 100]
内 -100 <= Node.val <= 100
c++代码实现
/**
* 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 path(TreeNode * node, vector<int>& pathVec, vector<string>& sPath) {
// 最后也要加入
pathVec.push_back(node->val);
// 停止条件
if (node->left == nullptr && node->right == nullptr) {
// 组合字符串
string str;
for (int i = 0 ; i < pathVec.size() - 1; i++) {
str += to_string(pathVec[i]);
str += "->";
}
str += to_string(pathVec[pathVec.size() - 1]);
sPath.push_back(str);
return;
}
// 单次遍历
if (node->left) {
path(node->left, pathVec, sPath);
// 回溯
pathVec.pop_back();
}
if (node->right) {
path(node->right, pathVec, sPath);
pathVec.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> pathVec;
vector<string> sPath;
path(root, pathVec, sPath);
return sPath;
}
};
python 代码实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.intPath = []
self.strPath = []
def path(self, node):
self.intPath.append(node.val)
# 停止条件
if not node.left and not node.right:
# 组合字符串
s = ""
for i in range(len(self.intPath)-1):
s += str(self.intPath[i])
s += "->"
s += str(self.intPath[len(self.intPath) - 1])
self.strPath.append(s)
return
if node.left:
self.path(node.left)
self.intPath.pop()
if node.right:
self.path(node.right)
self.intPath.pop()
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
self.path(root)
return self.strPath
404左叶子之和
给定二叉树的根节点 root
,返回所有左叶子之和。
示例 1:
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
示例 2:
输入: root = [1]
输出: 0
提示:
- 节点数在
[1, 1000]
范围内 -1000 <= Node.val <= 1000
c++ 代码实现
/**
* 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 leaves(TreeNode * node) {
// 停止条件
if (node == nullptr) return 0;
if (node->left == nullptr && node->right == nullptr) return 0;
// 计算
int leftVal = leaves(node->left);
// 左叶子不为空,但是其左右孩子为空,就是最后的左叶子
TreeNode * curNode = node->left;
if (curNode != nullptr &&curNode->left == nullptr && curNode->right == nullptr) {
leftVal = curNode->val;
}
// 计算
int rightVal = leaves(node->right);
int sum = leftVal + rightVal;
return sum;
}
int sumOfLeftLeaves(TreeNode* root) {
return leaves(root);
}
};
python 代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
if not root.left and not root.right:
return 0
lsum = self.sumOfLeftLeaves(root.left)
if root.left != None and root.left.left == None and root.left.right==None:
lsum = root.left.val
rsum = self.sumOfLeftLeaves(root.right)
return (lsum + rsum)