给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 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
可以使用递归法由下而上获得左右子树的高度,如发现左右子树高度差大于1,则不是平衡二叉树。平衡二叉树的左右子树都是平衡二叉树,反过来如左子树或右子树不是平衡二叉树,则其本身也不是平衡二叉树。可以用-1标记不是平衡二叉树的情况,一路回溯,最后结果为-1则不是平衡二叉树,反之则获得二叉树的高度并能确认其为平衡二叉树:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
return getHeight(root) != -1;
}
private int getHeight(TreeNode root) {
if (root == null) return 0;
int leftHeight = getHeight(root.left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(root.right);
if (rightHeight == -1) return -1;
if (Math.abs(leftHeight - rightHeight) > 1) return -1;
return Math.max(leftHeight,rightHeight) + 1;
}
}
这道题用迭代法就太复杂了,可以不考虑。
给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5] 输出:["1->2->5","1->3"]
示例 2:
输入:root = [1] 输出:["1"]
提示:
- 树中节点的数目在范围
[1, 100]
内 -100 <= Node.val <= 100
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) {
return res;
}
String path = "";
traversal(root, path, res);
return res;
}
private void traversal(TreeNode root, String path, List<String> res) {
path += root.val;
if (root.left == null && root.right == null) { // 叶子节点
res.add(path);
return;
}
if (root.left != null) {
traversal(root.left, path + "->", res);
}
if (root.right != null) {
traversal(root.right, path + "->", res);
}
}
}
其实,traversal(root.left, path + "->", res);中的path + "->"隐藏了回溯的写法。每次函数调用完,path依然是没有加上"->" 的,这就是回溯了。不隐藏回溯写法:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> path = new ArrayList<>();
traversal(root, path, res);
return res;
}
private void traversal(TreeNode root, List<Integer> path, List<String> res) {
path.add(root.val);
if (root.left == null && root.right == null) { // 叶子节点
String s = "";
for (int i = 0; i < path.size() - 1; i++) {
s += path.get(i).toString() + "->";
}
s += path.get(path.size() - 1).toString(); // 添加叶子节点,后面没有->
res.add(s);
}
if (root.left != null) {
traversal(root.left, path, res);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
traversal(root.right, path, res);
path.remove(path.size() - 1); // 回溯
}
}
}
用StringBuilder更快一点:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> path = new ArrayList<>();
traversal(root, path, res);
return res;
}
private void traversal(TreeNode root, List<Integer> path, List<String> res) {
path.add(root.val);
if (root.left == null && root.right == null) { // 叶子节点
StringBuilder sb = new StringBuilder();
for (int i = 0; i < path.size() - 1; i++) {
sb.append(path.get(i)).append("->");
}
sb.append(path.get(path.size() - 1)); // 添加叶子节点,后面没有->
res.add(sb.toString());
}
if (root.left != null) {
traversal(root.left, path, res);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
traversal(root.right, path, res);
path.remove(path.size() - 1); // 回溯
}
}
}
给定二叉树的根节点 root
,返回所有左叶子之和。
示例 1:
输入: root = [3,9,20,null,null,15,7] 输出: 24 解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
示例 2:
输入: root = [1] 输出: 0
提示:
- 节点数在
[1, 1000]
范围内 -1000 <= Node.val <= 1000
仅凭叶子节点本身,我们是无法判断它是左叶子还是右叶子的,所以在递归到叶子节点后,要回退到该叶子节点的双亲节点,然后判断双亲节点的左子树是否是叶子节点。也很简单,只要parentNode.left存在且没有左右孩子,即可判断其左子树是左叶子。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
if (root.left == null && root.right == null) // 叶子节点
return 0;
int leftValues = sumOfLeftLeaves(root.left); // 左子树的左叶子之和
int rightValues = sumOfLeftLeaves(root.right); // 右子树的左叶子之和
if (root.left != null && root.left.left == null && root.left.right == null) // 左叶子的双亲节点
leftValues = root.left.val; // 左叶子双亲节点的左孩子(左叶子)的值
return leftValues + rightValues; // 后序遍历,中
}
}
精简写法:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int leftValues = 0;
if (root.left != null && root.left.left == null && root.left.right == null) {
leftValues = root.left.val;
}
return leftValues + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
}
迭代法:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
Stack<TreeNode> stack = new Stack<> ();
stack.push(root);
int result = 0;
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null && node.left.left == null && node.left.right == null) {
result += node.left.val;
}
if (node.right != null) stack.add(node.right);
if (node.left != null) stack.add(node.left);
}
return result;
}
}