二叉树day6
110. 平衡二叉树
/**
* 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;
* }
* }
*/
//后序递归 满足条件返回高度不满足则返回-1
// class Solution {
// public boolean isBalanced(TreeNode root) {
// return postOrder(root) == -1 ? false : true;
// }
// private int postOrder(TreeNode root) {
// if (root == null) return 0;
// //左右中
// int lh = postOrder(root.left);
// if (lh == -1) return lh;
// int rh = postOrder(root.right);
// if (rh == -1) return rh;
// return Math.abs(lh - rh) <= 1 ? Math.max(lh, rh) + 1 : -1;
// }
// }
// //递归层序遍历每个节点,递归求子树最大深度
// class Solution {
// public boolean isBalanced(TreeNode root) {
// if (root == null) return true;
// if (Math.abs(getDepth(root.left) - getDepth(root.right)) > 1) return false;
// return isBalanced(root.left) && isBalanced(root.right);
// }
// private int getDepth(TreeNode root) {
// if (root == null) return 0;
// return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
// }
// }
//迭代层序遍历每个节点,后序求子树最大深度,最大深度即该结点高度
class Solution {
public boolean isBalanced(TreeNode root) {
Deque<TreeNode> queue = new LinkedList<>();
if (root != null) queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (Math.abs(getDepth(cur.left) - getDepth(cur.right)) <= 1) {
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
continue;
}
return false;
}
return true;
}
//递归
// private int getDepth(TreeNode root) {
// if (root == null) return 0;
// return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
// }
//迭代 用空指针标记已遍历未处理的结点
private int getDepth(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
if (root != null) stack.push(root);
int depth = 0, res = 0;
while (!stack.isEmpty()) {
TreeNode cur = stack.peek();
if (cur != null) {
//中右左 出栈左中右
depth++;
stack.push(null);
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
} else {
//空指针标记退栈
stack.pop();
cur = stack.pop();
depth--;
}
res = res > depth ? res : depth;
//以上过程可以在脑海中构造一个三个结点的满二叉树模拟一遍运行过程自然就懂了
}
return res;
}
}
257. 二叉树的所有路径
/**
* 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;
* }
* }
*/
//递归 List存路径
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) return res;
List<Integer> route = new ArrayList<>();
getRoute(root, res, route);
return res;
}
private void getRoute(TreeNode root, List<String> res, List<Integer> route) {
//中左右
route.add(root.val);
if (root.left == null && root.right == null) {
StringBuffer sb = new StringBuffer();
int size = route.size();
for (int i = 0; i < size - 1; i++) {
sb.append(route.get(i)).append("->");
}
sb.append(route.get(size - 1));
res.add(sb.toString());
}
if (root.left != null) {
getRoute(root.left, res, route);
//回溯
route.remove(route.size() - 1);
}
if (root.right != null) {
getRoute(root.right, res, route);
//回溯
route.remove(route.size() - 1);
}
}
}
//迭代前序
// class Solution {
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> res = new ArrayList<>();
// Deque<Object> stack = new LinkedList<>(); //存结点和到该结点的路径
// if (root == null) return res;
// stack.push(root);
// stack.push(root.val + "");
// while (!stack.isEmpty()) {
// //中
// String route = (String) stack.pop();
// TreeNode cur = (TreeNode) stack.pop();
// if (cur.left == null && cur.right == null) {
// res.add(route);
// }
// //右左 出栈左右 中已经处理
// if (cur.right != null) {
// stack.push(cur.right);
// stack.push(route + "->" + cur.right.val);
// }
// if (cur.left != null) {
// stack.push(cur.left);
// stack.push(route + "->" + cur.left.val);
// }
// }
// return res;
// }
// }
//前序递归,直接使用String +操作 “String+变量”因为编译时无法进行优化,所以这一条语句的操作是在运行时进行的,且会产生新的对象,而不是直接从 jvm 的 string常量池中获取
// class Solution {
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> res = new ArrayList<>();
// if (root == null) return res;
// String route = new String();
// getRoute(root, res, route);
// return res;
// }
// private void getRoute(TreeNode root, List<String> res, String route) {
// route += root.val + "";
// //中左右
// if (root.left == null && root.right == null) {
// res.add(route);
// }
// if (root.left != null) {
// getRoute(root.left, res, route + "->");
// }
// if (root.right != null) {
// getRoute(root.right, res, route + "->");
// }
// }
// }
//前序递归,使用StringBuffer存路径
// class Solution {
// public List<String> binaryTreePaths(TreeNode root) {
// List<String> res = new ArrayList<>();
// if (root == null) return res;
// StringBuffer route = new StringBuffer(root.val + "");
// getRoute(root, res, route);
// return res;
// }
// private void getRoute(TreeNode root, List<String> res, StringBuffer route) {
// //中左右
// if (root.left == null && root.right == null) {
// res.add(route.toString());
// }
// if (root.left != null) {
// StringBuffer rl = new StringBuffer(route.toString());
// rl.append("->" + root.left.val);
// getRoute(root.left, res, rl);
// }
// if (root.right != null) {
// StringBuffer rr = new StringBuffer(route.toString());
// rr.append("->" + root.right.val);
// getRoute(root.right, res, rr);
// }
// }
// }
404. 左叶子之和
//后序递归
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return postOrder(root);
}
private int postOrder(TreeNode root) {
if (root == null) return 0;
int sum = 0;
//左右中
if (root.left != null && root.left.left == null && root.left.right == null)
sum += postOrder(root.left) + root.left.val;
else
sum += postOrder(root.left);
if (root.right != null) sum += postOrder(root.right);
return sum;
}
}
迭代法,前中后序都可以,这里我用最容易理解的前序遍历。
//前序迭代
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
if (root != null) stack.push(root);
int res = 0;
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
if (cur.left != null && cur.left.left == null && cur.left.right == null)
res += cur.left.val;
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
}
return res;
}
}
为啥空间复杂度比迭代还高呢