平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
返回 true 。
public static boolean isBalanced(TreeNode root) {
int res= getHeight(root);
if (res==-1){
return false;
}
return true;
}
private static int getHeight(TreeNode root) {
if (root==null){
return 0;
}
//后续遍历 左右中
int left=getHeight(root.left);
int right=getHeight(root.right);
if (left==-1||right==-1){
return -1;
}
//左右子树绝对值大于1
if (Math.abs(left-right)>1){
return -1;
}
return Math.max(right,left)+1;
}
二叉树的所有路径
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root==null){
return result;
}
List<Integer> pathList = new ArrayList<>();//结果中的路径
backTracking(result,pathList,root);
return result;
}
private void backTracking(List<String> result, List<Integer> pathList, TreeNode root) {
pathList.add(root.val);
//叶子节点
if (root.left==null&&root.right==null){
StringBuilder builder = new StringBuilder();
for (int i = 0; i < pathList.size()-1; i++) {
builder.append(pathList.get(i)).append("->");
}
builder.append(pathList.get(pathList.size()-1));
result.add(builder.toString());
return;
}
//非叶子节点,同时进行回溯和递归
if (root.left!=null){
backTracking(result,pathList,root.left);
pathList.remove(pathList.size()-1);//清除这个左子节点
}
if (root.right!=null){
backTracking(result,pathList,root.right);
pathList.remove(pathList.size()-1);//清除这个左子节点
}
}
左叶子之和
计算给定二叉树的所有左叶子之和。
两个左叶子节点:9和15,返回24
public static int sumOfLeftLeaves(TreeNode root) {
if (root==null){
return 0;
}
int left=sumOfLeftLeaves(root.left);
int right=sumOfLeftLeaves(root.right);
int midValue=0;
if (root.left!=null&&root.left.left==null&&root.left.right==null){
midValue=root.left.val;
}
return left+right+midValue;
}
找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值。
输出:1
//迭代法
public int findBottomLeftValue(TreeNode root) {
int leftValue=0;
if (root==null){
return leftValue;
}
Queue<TreeNode> deque = new LinkedList<TreeNode>();
deque.offer(root);
while (!deque.isEmpty()){
int size=deque.size();
leftValue=deque.peek().val;
while (size>0){
TreeNode node = deque.poll();
if (node.left!=null) deque.offer(node.left);
if (node.right!=null) deque.offer(node.right);
size--;
}
}
return leftValue;
}
//递归法
public static int findBottomLeftValue2(TreeNode root){
value= root.val;
findLeftValue(root,0);
return value;
}
private static void findLeftValue(TreeNode root, int deep) {
if (root==null){
return;
}
if (root.left==null&&root.right==null){//叶子节点
if (deep>Deep){
value= root.val;
Deep=deep;
}
}
if (root.left!=null) findLeftValue(root.left,deep+1);
if (root.right!=null) findLeftValue(root.right,deep+1);
}
路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例: 给定如下二叉树,以及目标和 sum = 22,
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) {
return false;
}
targetSum -= root.val;
if (root.left == null && root.right == null) {
return targetSum == 0;
}
if (root.left != null) {
boolean left = hasPathSum(root.left, targetSum);
if (left) {
return true;
}
}
if (root.right != null) {
boolean right = hasPathSum(root.right, targetSum);
if (right) {
return true;
}
}
return false;
}
从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意: 你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树:
Map<Integer,Integer> map;
public TreeNode buildTree(int[] inorder, int[] postorder) {
map=new HashMap<>();
//用map保存中序序列的数值对应位置
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i],i);
}
//前闭后开
return findNode(inorder, 0, inorder.length, postorder,0, postorder.length);
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// 参数里的范围都是前闭后开
if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
root.left = findNode(inorder, inBegin, rootIndex,
postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder, rootIndex + 1, inEnd,
postorder, postBegin + lenOfLeft, postEnd - 1);
return root;
}