今日内容:110.平衡二叉树、257.二叉树的所有路径、404.左叶子之和 代码随想录链接:代码随想录 (programmercarl.com)
110.平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
按照前面的代码,我们可以计算每一个子树的高度差。
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null)return true;
int deep = deep(root);
if(deep == -1)return false;
return true;
}
private int deep(TreeNode root){
if(root == null)return 0;
int leftDepth = deep(root.left);
int rightDepth = deep(root.right);
if(leftDepth - rightDepth > 1 || rightDepth - leftDepth > 1 || leftDepth == -1 || rightDepth == -1){
return -1;
}
return Math.max(leftDepth,rightDepth)+1;
}
}
迭代法的效率比较低,再说。
257.二叉树的所有路径
给你一个二叉树的根节点
root,按 任意顺序 ,返回所有从根节点到叶子节点的路径。 叶子节点 是指没有子节点的节点。
返回到叶子节点的路径,首先就会涉及到判断是否为叶子节点,如果说先要遍历到叶子节点,再考虑记录路径。感觉可以用栈,刚好遍历也用栈。用前序遍历。
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null)
return result;
Stack<Object> stack = new Stack<>();
// 节点和路径同时入栈
stack.push(root);
stack.push(root.val + "");
while (!stack.isEmpty()) {
// 节点和路径同时出栈
String path = (String) stack.pop();
TreeNode node = (TreeNode) stack.pop();
// 若找到叶子节点
if (node.left == null && node.right == null) {
result.add(path);
}
//右子节点不为空
if (node.right != null) {
stack.push(node.right);
stack.push(path + "->" + node.right.val);
}
//左子节点不为空
if (node.left != null) {
stack.push(node.left);
stack.push(path + "->" + node.left.val);
}
}
return result;
}
}
递归写法
递归的时候要带着此时的路径,和保存路径的变量。
这里保存路径的变量就直接用输出的形式,通过一些代码把路径变成String。
一个注意的点:所以回溯要和递归永远在一起,世界上最遥远的距离是你在花括号里,而我在花括号外!
递归写法代码如下:
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String>res = new ArrayList<>();
if(root == null)return res;
List<Integer> paths = new ArrayList<>();
traversal(root, paths ,res);
return res;
}
private void traversal(TreeNode root, List<Integer> paths, List<String> res){
paths.add(root.val);
if(root.left == null && root.right == null){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < paths.size() - 1; i++){
sb.append(paths.get(i)).append("->");
}
sb.append(paths.get(paths.size() - 1));
res.add(sb.toString());
return;
}
if(root.left != null){
traversal(root.left, paths, res);
paths.remove(paths.size() - 1);
}
if(root.right != null){
traversal(root.right, paths, res);
paths.remove(paths.size() - 1);
}
}
}
或者精简一下
class Solution {
List<String> result = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
deal(root, "");
return result;
}
public void deal(TreeNode node, String s) {
if (node == null)
return;
if (node.left == null && node.right == null) {
result.add(new StringBuilder(s).append(node.val).toString());
return;
}
String tmp = new StringBuilder(s).append(node.val).append("->").toString();
deal(node.left, tmp);
deal(node.right, tmp);
}
}
404.左叶子之和
给定二叉树的根节点
root,返回所有左叶子之和。
求叶子结点的话,后序会方便些,用递归,然后加判定条件并求和。
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root == null)return 0;
int leftValue = sumOfLeftLeaves(root.left); // 左
int rightValue = sumOfLeftLeaves(root.right); // 右
int midValue = 0;
if (root.left != null && root.left.left == null && root.left.right == null) {
midValue = root.left.val;
}
int sum = midValue + leftValue + rightValue; // 中
return sum;
}
}
结束